ZetCode

Java StrictMath 类

最后修改时间:2025 年 4 月 13 日

java.lang.StrictMath 类提供用于执行具有严格浮点精度基本数值运算的方法。 与 Math 不同,StrictMath 保证在所有平台上产生完全相同的位结果。 这使其成为需要可重现结果的应用程序的理想选择。

StrictMath 中的所有方法都是静态的,包括三角函数、指数函数、对数函数和其他常见的数学函数。 该类通过使用来自著名的自由分发数学库 (fdlibm) 的算法来确保平台独立性。

StrictMath 类方法

StrictMath 类包含 70 多个用于数学运算的方法。 这些方法包括基本算术、三角学、指数、舍入和特殊函数。 所有方法都是静态的,并使用原始数值类型。

public final class StrictMath {
    public static double sin(double a) {...}
    public static double cos(double a) {...}
    public static double tan(double a) {...}
    public static double exp(double a) {...}
    public static double log(double a) {...}
    public static double sqrt(double a) {...}
    public static double pow(double a, double b) {...}
    public static int abs(int a) {...}
    public static double ceil(double a) {...}
    public static double floor(double a) {...}
    // ... and many more
}

上面的代码显示了 StrictMath 中可用方法的一个子集。 这些方法提供具有严格浮点语义的数学运算,确保在不同平台上的结果一致。

基本算术运算

StrictMath 提供了用于基本算术运算的方法,例如绝对值、最大值、最小值和符号。 这些方法适用于各种原始类型,并确保在不同平台上的结果一致。

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        // Absolute value
        int absInt = StrictMath.abs(-10);
        double absDouble = StrictMath.abs(-3.14);
        
        // Maximum and minimum
        double max = StrictMath.max(5.6, 9.2);
        double min = StrictMath.min(5.6, 9.2);
        
        // Signum function
        double signum = StrictMath.signum(-25.5);
        
        System.out.println("Absolute int: " + absInt);
        System.out.println("Absolute double: " + absDouble);
        System.out.println("Maximum: " + max);
        System.out.println("Minimum: " + min);
        System.out.println("Signum: " + signum);
    }
}

此示例演示了使用 StrictMath 的基本算术运算。 abs 方法返回绝对值,maxmin 比较值,signum 返回数字的符号 (-1、0 或 1)。 所有操作都以严格的精度执行。

三角函数

StrictMath 提供了标准的三角函数,包括正弦、余弦、正切及其逆函数。 这些方法以弧度为单位接受角度,并根据严格的浮点规则返回精确的结果。

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        double angle = StrictMath.PI / 4; // 45 degrees in radians
        
        // Basic trigonometric functions
        double sinValue = StrictMath.sin(angle);
        double cosValue = StrictMath.cos(angle);
        double tanValue = StrictMath.tan(angle);
        
        // Inverse trigonometric functions
        double asinValue = StrictMath.asin(sinValue);
        double acosValue = StrictMath.acos(cosValue);
        double atanValue = StrictMath.atan(tanValue);
        
        System.out.println("Sine: " + sinValue);
        System.out.println("Cosine: " + cosValue);
        System.out.println("Tangent: " + tanValue);
        System.out.println("Arcsine: " + asinValue);
        System.out.println("Arccosine: " + acosValue);
        System.out.println("Arctangent: " + atanValue);
    }
}

此示例显示了使用 StrictMath 的三角运算。 我们计算 45 度角(π/4 弧度)的正弦、余弦和正切,然后使用反函数验证结果。 保证在所有平台上保持一致的精度。

指数和对数函数

StrictMath 包括用于指数和对数计算的方法。 这些包括自然对数、以 10 为底的对数、指数函数和幂运算。 所有方法都保持严格的浮点语义。

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        double value = 2.5;
        
        // Exponential and logarithmic functions
        double exp = StrictMath.exp(value);
        double log = StrictMath.log(value);
        double log10 = StrictMath.log10(value);
        
        // Power functions
        double pow = StrictMath.pow(value, 3);
        double sqrt = StrictMath.sqrt(value);
        
        System.out.println("exp(" + value + "): " + exp);
        System.out.println("ln(" + value + "): " + log);
        System.out.println("log10(" + value + "): " + log10);
        System.out.println(value + "^3: " + pow);
        System.out.println("sqrt(" + value + "): " + sqrt);
    }
}

此示例演示了指数和对数函数。 我们计算 e2.5、自然对数、以 10 为底的对数、2.53 和 2.5 的平方根。 所有操作都以 StrictMath 保证的严格浮点精度执行。

舍入和余数运算

StrictMath 提供了几种用于舍入数字和计算余数的方法。 这些包括上限、下限、四舍五入和 IEEE 余数运算。 每个方法都遵循严格的浮点规则。

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        double num1 = 3.7;
        double num2 = -2.3;
        
        // Rounding operations
        double ceil1 = StrictMath.ceil(num1);
        double floor1 = StrictMath.floor(num1);
        long round1 = StrictMath.round(num1);
        
        double ceil2 = StrictMath.ceil(num2);
        double floor2 = StrictMath.floor(num2);
        long round2 = StrictMath.round(num2);
        
        // Remainder operation
        double remainder = StrictMath.IEEEremainder(10, 3);
        
        System.out.println("Ceiling of " + num1 + ": " + ceil1);
        System.out.println("Floor of " + num1 + ": " + floor1);
        System.out.println("Round of " + num1 + ": " + round1);
        System.out.println("Ceiling of " + num2 + ": " + ceil2);
        System.out.println("Floor of " + num2 + ": " + floor2);
        System.out.println("Round of " + num2 + ": " + round2);
        System.out.println("IEEE remainder of 10/3: " + remainder);
    }
}

此示例显示了使用 StrictMath 的舍入运算。 ceil 向上舍入,floor 向下舍入,round 舍入到最接近的整数。 IEEEremainder 方法根据 IEEE 754 标准计算余数。

双曲函数

StrictMath 包括双曲三角函数:双曲正弦、余弦和正切。 这些在各种工程和物理应用中很有用,并保持严格的浮点精度。

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        double x = 1.5;
        
        // Hyperbolic functions
        double sinh = StrictMath.sinh(x);
        double cosh = StrictMath.cosh(x);
        double tanh = StrictMath.tanh(x);
        
        System.out.println("Hyperbolic sine: " + sinh);
        System.out.println("Hyperbolic cosine: " + cosh);
        System.out.println("Hyperbolic tangent: " + tanh);
        
        // Verify identity: cosh^2(x) - sinh^2(x) = 1
        double identity = cosh * cosh - sinh * sinh;
        System.out.println("cosh^2(x) - sinh^2(x) = " + identity);
    }
}

此示例演示了 StrictMath 中的双曲函数。 我们计算 1.5 的双曲正弦、余弦和正切,然后验证基本恒等式 cosh²(x) - sinh²(x) = 1。 结果显示了 StrictMath 运算的精度。

角度转换

StrictMath 提供了用于在度和弧度之间进行转换的方法。 当使用需要以弧度为单位的角度的三角函数时,这些转换至关重要。

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        double degrees = 45.0;
        double radians = StrictMath.toRadians(degrees);
        
        System.out.println(degrees + " degrees = " + radians + " radians");
        
        // Convert back to degrees
        double backToDegrees = StrictMath.toDegrees(radians);
        System.out.println(radians + " radians = " + backToDegrees + " degrees");
        
        // Calculate sin of 45 degrees
        double sin45 = StrictMath.sin(radians);
        System.out.println("sin(45°) = " + sin45);
    }
}

此示例显示了使用 StrictMath 在度和弧度之间进行角度转换。 我们将 45 度转换为弧度,然后转换回度,最后通过首先转换为弧度来计算 45 度的正弦值。 在所有转换过程中都保持了精度。

随机数生成

StrictMath 包括一个随机数生成器方法,该方法以严格的浮点语义生成伪随机数。 这些数字在 0.0(含)和 1.0(不含)之间均匀分布。

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        // Generate 5 random numbers
        System.out.println("Random numbers:");
        for (int i = 0; i < 5; i++) {
            System.out.println(StrictMath.random());
        }
        
        // Generate random integer between 1 and 100
        int min = 1;
        int max = 100;
        int randomInt = min + (int)(StrictMath.random() * ((max - min) + 1));
        System.out.println("Random integer between " + min + " and " + max + 
                         ": " + randomInt);
    }
}

此示例演示了使用 StrictMath 进行随机数生成。 我们首先生成五个介于 0.0 和 1.0 之间的随机双精度数,然后展示如何生成特定范围内的随机整数。 由于严格的浮点规则,结果在不同平台上是可重现的。

来源

Java StrictMath 类文档

在本文中,我们通过实际示例介绍了 Java StrictMath 类的重要方法。 了解这些方法对于需要严格浮点精度和可重现性的应用程序非常重要。

作者

我的名字是 Jan Bodnar,是一位经验丰富的程序员,在编程领域拥有多年经验。 我于 2007 年开始撰写编程文章,此后撰写了 1,400 多篇文章和 8 本电子书。 凭借 8 年以上的教学经验,我致力于分享我的知识并帮助他人掌握编程概念。

列出所有Java教程