StrictMath Bit For Bit Reproducible

StrictMath pins transcendental functions to published fdlibm 5.3 algorithms so results are bit-for-bit identical on every platform; Math trades that for speed.

JavaData Integrity

Code

snippet
public class StrictMathDemo {

    public static void main(String[] args) {
        // StrictMath:fdlibm 5.3 算法的 Java 转写,任何平台逐位一致
        double s = StrictMath.sin(0.5);
        double c = StrictMath.cos(0.5);
        double p = StrictMath.pow(2.0, 10.0);
        double e = StrictMath.expm1(0.01);
        System.out.println("sin:  " + s);
        System.out.println("pow:  " + p);
        System.out.println("expm1:" + e);

        // Math:默认多数方法直接委托 StrictMath,但允许平台用更快实现,
        // 不保证逐位一致;常见场景下两者给出同一个值
        double ms = Math.sin(0.5);
        System.out.println("Math.sin same as StrictMath.sin: " + (ms == s));

        // hypot 避免中间溢出:两条直角边各取极大值仍能得到正确斜边
        double hyp = StrictMath.hypot(1.0e300, 1.0e300);
        System.out.println("hypot: " + hyp);

        // 基本运算(+ - * /)两版没有差别:差异只来自超越函数
        System.out.println("arithmetic identical: " + (0.1 + 0.2 == StrictMath.abs(-0.30000000000000004)));
    }
}

How it works

Structurally, expect the class StrictMathDemo and the function main and nothing else.

Points of care

  • The language toolchain itself is the only prerequisite.

Applying it

Entry signature: public static void main(String[] args)

Watch for labeled lines in the run — sin:, pow:, expm1: — when the file runs.

Statements that matter

double hyp = StrictMath.hypot(1.0e300, 1.0e300);
double p = StrictMath.pow(2.0, 10.0);
double e = StrictMath.expm1(0.01);

Interactive Tools for This Topic

FAQ

What does StrictMath Bit For Bit Reproducible print when run?
Run it as a complete program — the output lines carry labels such as `sin:`, `pow:`, `expm1:`.