7.3.4 Math类
Java
提供了基本的加
(+
)、减(-
)、乘(*
)、除(/
)、求余(%
)等基本算术运算的运算符,但对于更复杂的数学运算,例如,三角函数、对数运算、指数运算等则无能为力。Java
提供了Math
工具类来完成这些复杂的运算,Math
类是一个工具类,它的构造器被定义成private
的,因此无法创建Math
类的对象;Math
类中的所有方法都是类方法,可以直接通过类名来调用它们。Math
类除提供了大量静态方法之外,还提供了两个类变量:PI
和E
,它们的值分别等于π
和e
。
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| public class MathTest { public static void main(String[] args) { System.out.println("Math.toDegrees(1.57):" + Math.toDegrees(1.57)); System.out.println("Math.toRadians(90):" + Math.toRadians(90)); System.out.println("Math.acos(1.2):" + Math.acos(1.2)); System.out.println("Math.asin(0.8):" + Math.asin(0.8)); System.out.println("Math.atan(2.3):" + Math.atan(2.3)); System.out.println("Math.cos(1.57):" + Math.cos(1.57)); System.out.println("Math.cosh(1.2 ):" + Math.cosh(1.2)); System.out.println("Math.sin(1.57 ):" + Math.sin(1.57)); System.out.println("Math.sinh(1.2 ):" + Math.sinh(1.2)); System.out.println("Math.tan(0.8 ):" + Math.tan(0.8)); System.out.println("Math.tanh(2.1 ):" + Math.tanh(2.1)); System.out.println("Math.atan2(0.1, 0.2):" + Math.atan2(0.1, 0.2)); System.out.println("Math.floor(-1.2 ):" + Math.floor(-1.2)); System.out.println("Math.ceil(1.2):" + Math.ceil(1.2)); System.out.println("Math.round(2.3 ):" + Math.round(2.3)); System.out.println("Math.sqrt(2.3 ):" + Math.sqrt(2.3)); System.out.println("Math.cbrt(9):" + Math.cbrt(9)); System.out.println("Math.exp(2):" + Math.exp(2)); System.out.println("Math.hypot(4 , 4):" + Math.hypot(4, 4)); System.out.println("Math.IEEEremainder(5 , 2):" + Math.IEEEremainder(5, 2)); System.out.println("Math.pow(3, 2):" + Math.pow(3, 2)); System.out.println("Math.log(12):" + Math.log(12)); System.out.println("Math.log10(9):" + Math.log10(9)); System.out.println("Math.log1p(9):" + Math.log1p(9)); System.out.println("Math.abs(-4.5):" + Math.abs(-4.5)); System.out.println("Math.copySign(1.2, -1.0):" + Math.copySign(1.2, -1.0)); System.out.println("Math.signum(2.3):" + Math.signum(2.3)); System.out.println("Math.max(2.3 , 4.5):" + Math.max(2.3, 4.5)); System.out.println("Math.min(1.2 , 3.4):" + Math.min(1.2, 3.4)); System.out.println("Math.nextAfter(1.2, 1.0):" + Math.nextAfter(1.2, 1.0)); System.out.println("Math.nextUp(1.2 ):" + Math.nextUp(1.2)); System.out.println("Math.random():" + Math.random()); } }
|
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Math.toDegrees(1.57):89.95437383553926 Math.toRadians(90):1.5707963267948966 Math.acos(1.2):NaN Math.sqrt(2.3 ):1.51657508881031 Math.cbrt(9):2.080083823051904 Math.exp(2):7.38905609893065 Math.hypot(4 , 4):5.656854249492381 Math.IEEEremainder(5 , 2):1.0 Math.pow(3, 2):9.0 Math.log(12):2.4849066497880004 Math.log10(9):0.9542425094393249 Math.log1p(9):2.302585092994046 Math.abs(-4.5):4.5 Math.copySign(1.2, -1.0):-1.2 Math.signum(2.3):1.0 Math.max(2.3 , 4.5):4.5 Math.min(1.2 , 3.4):1.2 Math.nextAfter(1.2, 1.0):1.1999999999999997 Math.nextUp(1.2 ):1.2000000000000002 Math.random():0.06276607672087764
|
原文链接: 7.3.4 Math类