What's the difference between java.lang.Math and java.lang.StrictMath?

后端 未结 4 1953
梦毁少年i
梦毁少年i 2020-12-04 14:55

Obviously java.lang.StrictMath contains additional functions (hyperbolics etc.) which java.lang.Math doesn\'t, but is there a difference in the functions which are found in

4条回答
  •  温柔的废话
    2020-12-04 15:54

    The Javadoc for the Math class provides some information on the differences between the two classes:

    Unlike some of the numeric methods of class StrictMath, all implementations of the equivalent functions of class Math are not defined to return the bit-for-bit same results. This relaxation permits better-performing implementations where strict reproducibility is not required.

    By default many of the Math methods simply call the equivalent method in StrictMath for their implementation. Code generators are encouraged to use platform-specific native libraries or microprocessor instructions, where available, to provide higher-performance implementations of Math methods. Such higher-performance implementations still must conform to the specification for Math.

    Therefore, the Math class lays out some rules about what certain operations should do, but they do not demand that the exact same results be returned in all implementations of the libraries.

    This allows for specific implementations of the libraries to return similiar, but not the exact same result if, for example, the Math.cos class is called. This would allow for platform-specific implementations (such as using x86 floating point and, say, SPARC floating point) which may return different results.

    (Refer to the Software Implementations section of the Sine article in Wikipedia for some examples of platform-specific implementations.)

    However, with StrictMath, the results returned by different implementations must return the same result. This would be desirable for instances where the reproducibility of results on different platforms are required.

提交回复
热议问题