Java的Math类提供了许多用于数学计算的常用API,这些API包括但不限于以下内容:
示例代码:
double a = -2.5; double b = 3.4; int c = 5; int d = -10; double absA = Math.abs(a); double ceilB = Math.ceil(b); double floorA = Math.floor(a); double maxCAndD = Math.max(c, d); double minAAndB = Math.min(a, b); double powC2 = Math.pow(c, 2); double sqrtB = Math.sqrt(b); double random = Math.random();
上述代码将返回以下结果:
absA = 2.5 ceilB = 4.0 floorA = -3.0 maxCAndD = 5 minAAndB = -2.5 powC2 = 25.0 sqrtB = 1.843908891458577 random = 0.8147236979815794
除了这些API外,Math类还提供了许多其他有用的数学函数,例如三角函数和指数函数。
以下是Math类提供的一些其他常用API:
double angle = Math.PI / 4; // 45度 double sinAngle = Math.sin(angle); double cosAngle = Math.cos(angle); double tanAngle = Math.tan(angle); double asinAngle = Math.asin(sinAngle); double acosAngle = Math.acos(cosAngle); double atanAngle = Math.atan(tanAngle); double expValue = Math.exp(2); double logValue = Math.log(10); double log10Value = Math.log10(100); double degrees = Math.toDegrees(Math.PI / 2); // 90度 double radians = Math.toRadians(180); // π弧度
sinAngle = 0.7071067811865475 cosAngle = 0.7071067811865476 tanAngle = 0.9999999999999999 asinAngle = 0.7853981633974483 acosAngle = 0.7853981633974484 atanAngle = 0.9999999999999999 expValue = 7.3890560989306495 logValue = 2.302585092994046 log10Value = 2.0 degrees = 90.0 radians = 3.141592653589793
Math类的API非常广泛,可用于各种数学计算,尤其在科学和工程领域中非常有用。
除了上述提到的常用API外,Math类还提供了许多其他API,包括:
double side1 = 3; double side2 = 4; double hypotenuse = Math.hypot(side1, side2); double sign1 = Math.signum(side1); double sign2 = Math.signum(0); double atan2Value = Math.atan2(side1, side2); double cbrtValue = Math.cbrt(27); int floorDivResult = Math.floorDiv(10, 3); int floorModResult = Math.floorMod(10, 3); double nextAfterResult = Math.nextAfter(3.0, 4.0); double nextUpResult = Math.nextUp(2.0); double ulpResult = Math.ulp(1.0); int exponent = Math.getExponent(1024.0);
hypotenuse = 5.0 sign1 = 1.0 sign2 = 0.0 atan2Value = 0.6435011087932844 cbrtValue = 3.0 floorDivResult = 3 floorModResult = 1 nextAfterResult = 3.0000000000000004 nextUpResult = 2.0000000000000004 ulpResult = 2.220446049250313E-16 exponent = 10
Math类提供的API非常丰富,可以满足各种数学计算的需求。
原文链接:codingdict.net