. lang。数学库提供了常用的数学计算工具
最终双E=2.7182818284590452354;//自然对数底数 最后两个π=3.14159265358979323846;//圆周率 最终双DEGREES_TO_RADIANS=0.017453292519943295;//角度转弧度 最终双RADIANS_TO_DEGREES=57.29577951308232;//弧度转角度
-
<李> abs (x):绝对值李>
<李>地板(x):向下取整李>
<李>装天花板(x):向上取整李>
<李>圆(x):四舍五入,如果有两个(x.5),返回较大的那个数李>
<李>无线电侦察(x):最接近的整数,如果有两个(x.5),返回偶数李>
<李> floorDiv (x, y):向下取整除法李>
<李> floorMod (x, y): java默认的取摸%得到的结果和x的符号相同,floorMod和y的符号相同李>
双δ=0.0000001; assertequal (Math.abs (6), 6); assertequal (Math.floor(-6.2), 7,δ);//向下取整 assertequal (Math.floor(6.2)、6δ); assertequal (Math.floor(6.8)、6δ); assertequal (Math.ceil(-6.2)、6δ);//向上取整 assertequal (Math.ceil(6.2), 7,δ); assertequal (Math.ceil(6.8), 7,δ); assertequal (Math.round(-6.2)、6δ);//四舍五入 assertequal (Math.round(6.2)、6δ); assertequal (Math.round(6.8), 7,δ); assertequal (Math.round(-6.5)、6δ); assertequal (Math.round(6.5), 7,δ); assertequal (Math.rint(-6.2)、6δ);//最接近整数,如果存在两个,返回偶数 assertequal (Math.rint(6.2)、6δ); assertequal (Math.rint(6.8), 7,δ); assertequal (Math.rint(-6.5)、6δ); assertequal (Math.rint(6.5)、6δ); assertequal(数学。floorDiv (7,3), 2); assertequal(数学。floorDiv (7,3), 3); assertequal(数学。floorMod (7,3), 1); assertequal(数学。floorMod (7,3), 1); assertequal(数学。floorMod (7,3), 2); assertequal (3 -7%, 1); assertequal (3 -7%, 1); >之前
assertequal (sin(数学。π/2),1.0,δ); assertequal (Math.cos (Math.PI) 1δ); assertequal (Math.tan(数学。π/4),1.0,δ); assertequal (Math.asin(1),数学。π/2,δ); assertequal (Math.acos(1),数学。π,δ); assertequal (Math.atan(1),数学。π/4,δ); >之前
-
<李>战俘(x, y): x ^ y, y的次方李>
<李> sqrt (x):√x, x的平方根李>
<李> cbrt (x):三次方根李>
<李>函数(x, y):√(x²李+ y²) >
<李> exp (x): e x ^ 李>
<李> expm1 (x): e ^ x - 1 李>
<李>日志(x): ln (x) 李>
<李> log10: lg (x) 李>
<李> log1p (x): ln (1 + x) 李>
assertequal(数学。战俘(3,2),δ); assertequal(数学。战俘(2、3),8,δ); assertequal (Math.sqrt(4) 2δ); assertequal (Math.cbrt(27) 3δ); assertequal(数学。函数(3、4),5、δ);//?x²+ y²) assertequal (Math.exp (2.5)、Math.pow(数学。E, 2.5),δ);//e ^ x Math.exp assertequal(使用(2),(2)- 1,δ);//e ^ x - 1 assertequal (Math.log (Math.exp(1.5)), 1.5,δ);//ln (x) assertequal (log10(1000), 3,δ);//lg (x) assertequal (Math.log1p(数学。E - 1), 1,δ);//ln (1 + x) >之前
-
<李> sinh (x):(e x - e ^ ^ - x)/2 李>
<李> cosh (x):(e ^ ^ x + e - x)/2 李>
<李>双曲正切(x): sinh (x)/cosh (x) 李>
assertequal (Math.sinh (2), (Math.exp (2) - Math.exp(2))/2,δ);//sinh (x)=(e x - e ^ ^ - x)/2 assertequal (Math.cosh (2), (Math.exp (2) + Math.exp(2))/2,δ);//cosh (x)=x + e (e ^ ^ - x)/2 Math.sinh assertequal (Math.tanh (2), (2)/Math.cosh(2),δ);//双曲正切(x)=sinh (x)/cosh (x) >之前
普通的数值计算在溢出时是没有感知的,比如长。MAX_VALUE + 1将得到结果长。MIN_VALUE,为了解决这种不合理,数学提供了一些辅助函数,在结果溢出时将抛出异常
-
<李> addExact (x, y):加法李>
<李> multiplyExact (x, y):乘法李>
<李> decrementExact (x, y):递减李>
<李> incrementExact (x, y):递增李>
<李> negateExact (x, y):相反数李>
<李> multiplyFull (x, y):接受两个int返回一个长,防止溢出李>
<李> multiplyHigh (x, y):返回两个长乘积的高64位李>
assertequal(长。MAX_VALUE + 1, Long.MIN_VALUE);//溢出 assertThrows (ArithmeticException。类,()→Math.addExact(长。MAX_VALUE, 1));//加法溢出抛异常 assertThrows (ArithmeticException。类,()→Math.multiplyExact(长。MAX_VALUE, 2));//乘法 assertThrows (ArithmeticException。类,()→Math.decrementExact (Long.MIN_VALUE));//递减 assertThrows (ArithmeticException。类,()→Math.incrementExact (Long.MAX_VALUE));//递增 assertThrows (ArithmeticException。类,()→Math.negateExact (Long.MIN_VALUE));//相反数 assertequal(数学。addExact (1、2)、3); assertequal(数学。multiplyExact (2,3), 6); assertequal (Math.incrementExact (6)、7); assertequal (Math.decrementExact (6), 5); assertequal (Math.negateExact (6), 6); assertequal(数学。multiplyFull (1、2), 2);//接受两个int返回一个长,防止溢出 assertequal(数学。multiplyHigh (1、2), 0);//返回两个长乘积的高64位java数学计算的具体使用