Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The StrictMath class

Reference for java.lang.StrictMath: bit-for-bit reproducible math functions based on fdlibm, how it differs from Math, and when you should use it.

StrictMath has almost the same methods as Math: sin, cos, pow, sqrt, exp, log, floor and so on. The difference is a guarantee. Every StrictMath method must return exactly the result of the published fdlibm algorithms, so the same input gives the same bits on every JVM, operating system and CPU.

Math is allowed to be a little less exact (within 1 or 2 ulps for most functions) so the JVM can use fast hardware instructions or intrinsics. In practice the two give identical output for the vast majority of inputs, but not for all of them. Use StrictMath when you need results that can be compared byte for byte across machines, for example in lock-step simulations, golden-file tests, or file formats that store computed values.

Java
double a = StrictMath.sin(1.0);
double b = Math.sin(1.0); // usually, but not guaranteed to be, identical

Try it

Java The same API as Math

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Comparing Math and StrictMath

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public final class StrictMath
Member Description
PI, E The same constants as Math.
sin, cos, tan, asin, acos, atan, atan2 Trigonometric functions in radians, fdlibm results.
sinh, cosh, tanh Hyperbolic functions.
exp, expm1, log, log10, log1p Exponentials and logarithms.
pow, sqrt, cbrt, hypot Powers and roots.
floor, ceil, rint, round, abs, min, max, signum Rounding and comparison, specified exactly in both classes.
addExact, multiplyExact, floorDiv, floorMod, fma ... Integer helpers, also mirrored from Math.

There is no constructor. Parameters and return types match the Math method of the same name. It does not offer Math.random().

Usage notes

  • Same results everywhere. Since Java 17 all floating-point arithmetic is strict (JEP 306), so ordinary + - * / is reproducible by default. StrictMath extends that promise to library functions.
  • Math may differ. Math methods are only required to stay within a documented error bound and to be semi-monotonic, so intrinsics may produce a different last bit on a different CPU or JVM.
  • Some methods are identical by definition. sqrt, floor, ceil, rint, abs, min, max and fma are exactly rounded or exactly defined in both classes, so choosing one over the other changes nothing there.
  • Speed. Math can be several times faster for trigonometric and exponential functions. Do not switch to StrictMath unless reproducibility is a real requirement.
  • Inside Math. Many Math methods simply delegate to StrictMath when no faster intrinsic exists, which is why results usually agree.
  • Beyond one language. StrictMath matches fdlibm, not the C library of your machine, so results can still differ from C, Python or JavaScript code.
  • Thread safety. All methods are pure and safe to call concurrently.