一、java.lang.Math
public final class Math,该类被声明为final,表示不能够被继承。
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。在编写不同类别的程序时,可能需要的函数也不同。
1.平方根函数sqrt 返回正确舍入的 double
值的正平方根。详细说明及测试代码见附录一
2.立方根函数cqrt 返回 double
值的立方根。详细说明及测试代码见附录二
3.幂值运算pow 返回第一个参数的第二个参数次幂的值。public static double pow(double a , double b)
4.取最值运算max 返回两个数中较大的数;min返回两个数中较小的数
5.绝对值运算abs 返回参数的绝对值
6.Math类提供了一些常用的三角函数:
Math.sin 返回角的三角正弦。
Math.cos 返回角的三角余弦。
Math.tan 返回角的三角正切。
Math.asin 返回一个值的反正弦。
Math.acos 返回一个值的反余弦。
Math.atan 返回一个值的反正切。
Math.atan2 将矩形坐标 (x
, y
) 转换成极坐标 (r, theta),返回所得角 theta。
7.还有指数参数以及他们的反函数:
Math.exp 返回欧拉数 e 的 double
次幂的值。
Math.log 返回 double
值的自然对数(底数是 e)。
Math.log10 返回 double
值的底数为 10 的对数。 Math.log10(100)=2.0
8.求近似值运算round 详细说明及测试代码见附录三
9.最后在Math类中还提供了两个用于表示π和e常量的近似值:
public static final double E = 2.7182818284590452354; public static final double PI = 3.14159265358979323846;
public static double sqrt(double a)
double
值的正平方根。特殊情况如下:
double
值。
a
- 一个值。
a
的正平方根。如果参数为 NaN 或小于 0,那么结果为 NaN。
System.out.println(Math.sqrt(16)); System.out.println(Math.sqrt(-16)); System.out.println(Math.sqrt(-0)); System.out.println(Math.sqrt(+0)); System.out.println(Math.sqrt(Double.MAX_VALUE*Double.MAX_VALUE));
结果为:
4.0 NaN 0.0 0.0 Infinity(无穷大)
public static double cbrt(double a)
double
值的立方根。对于正的有限值
x
,
cbrt(-x) == -cbrt(x)
;也就是说,负值的立方根是该值数值的负立方根。特殊情况如下:
计算结果必须在准确结果的 1 ulp 范围内。
a
- 一个值。
a
的立方根。
附录三:round
public static long round(double a)
long
。结果将舍入为整数:加上 1/2,对结果调用 floor 并将所得结果强制转换为
long
类型。换句话说,结果等于以下表达式的值:
(long)Math.floor(a + 0.5d)
特殊情况如下:
Long.MIN_VALUE
的值,那么结果等于 Long.MIN_VALUE
的值。 Long.MAX_VALUE
的值,那么结果等于 Long.MAX_VALUE
的值。a
- 舍入为
long
的浮点值。
long
值的参数值。
System.out.println(Math.round(15.5));//计算结果:16 System.out.println(Math.round(15.51));//计算结果:16 System.out.println(Math.round(15.6));//计算结果:16 System.out.println(Math.round(15.2356));//计算结果:15 System.out.println(Math.round(-15.5));//计算结果:-15 System.out.println(Math.round(-15.51));//计算结果:-16 System.out.println(Math.round(-15.6));//计算结果:-16
通过计算结果发现,Java中的Math.round()方法特点:如果是负数,其小数位的数值小于等于5的话,那么不会进位,如果大于了5,才会进位。
二、大数字操作类(数字超过了double的范围)
大整数操作类:BigInteger
大整数可以操作无限大的整数类型数据
NO |
方法 |
描述 |
1 |
Public BigInteger(String val) |
实例化BigInteger对象 |
2 |
Public BigInteger add(BigInteger val) |
加法 |
3 |
Public BigInteger subtract(BigInteger val) |
减法 |
4 |
Public BigInteger multiply(BigInteger val) |
乘法 |
5 |
Public BigInteger divide(BigInteger val) |
除法 |
6 |
Public BigInteger[] divideAndRemainder(BigInteger val) |
除法(保留余数),数组第一个元素是商,第二个元素是余数。 |
import java.math.BigInteger; public class BigIntegerTest { public static void main(String[] args) { BigInteger bigA = new BigInteger("234809234801"); BigInteger bigB = new BigInteger("8939834789"); System.out.println("加法结果是" + bigA.add(bigB)); System.out.println("减法结果是" + bigA.subtract(bigB)); System.out.println("乘法结果是" + bigA.multiply(bigB)); System.out.println("除法结果是" + bigA.divide(bigB)); BigInteger[] res = bigA.divideAndRemainder(bigB); System.out.println("商:" + res[0] + " , 余数:" + res[1]); } }
结果:
<pre class="html" name="code">加法结果是243749069590 减法结果是225869400012 乘法结果是2099155766052449291989 除法结果是26 商:26 , 余数:2373530287
大数的小数操作类:BigDecimal(Decimal 小数的,十进制的)
NO |
方法 |
描述 |
1 |
Public static final int ROUND_HALF_UP |
常量 向上进位 |
2 |
Public BigDecimal(double val) |
构造 传递一个double型数据 |
3 |
Public BigDecimal divide(BigDecimal divisor,int scale, int roundingModde) |
除法操作,设置好保留小数位及进位模式 |
示例:完成准确位的四舍五入操作
import java.math.BigDecimal; public class BigIntegerTest2 { public static void main(String[] args) { System.out.println(MyMath.round(15.5 , 0)); System.out.println(MyMath.round(-15.5 , 0)); System.out.println(MyMath.round(168.98765 , 2)); } } class MyMath { public static double round(double num , int scale) { BigDecimal big = new BigDecimal(num); BigDecimal result = big.divide(new BigDecimal(1) ,scale ,BigDecimal.ROUND_HALF_UP); return result.doubleValue(); } }
结果:
16.0 -16.0 168.99