1

I know that StrictMath follows given algorithms and will give the same result on every machine; however Math is more accurate and I see it being used more. Which is preferred? I would like reproducibility obviously, but also accuracy matters a great deal to me as well.

Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
  • Why do you think `Math` is more accurate? It’s rather likely to be *faster* due to the lesser compatibility constraints. – Holger Oct 06 '15 at 18:35
  • The API says "This relaxation [of `Math` as opposed to `StrictMath`] permits better-performing implementations where strict reproducibility is not required." I presumed better-performing meant more accurate but I might have misinterpreted better-performing where it actually meant faster. – Eli Sadoff Oct 06 '15 at 18:48

2 Answers2

3

Based on true definition of accuracy, StrictMath is more accurate. Generally speaking Math is faster than StrictMath, but not always the case; often, the Math library functions simply calls a StrictMath implementation.

In any event, I think the answer to your question can be found straight from the Math Javadoc:

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.

For most applications, the general Math library is the better choice. If very small roundoff errors are a concern, then StrictMath is necessary. So don't get wrapped around the axle about inconsistency unless your accuracy must be very strong.

Keith
  • 3,079
  • 2
  • 17
  • 26
0

I guess you have answer to your own question. You just have to choose based on your requirements. I disagree that Math is generally faster than StrictMath. In some case yes, in some case not (depending on the operation). For this reason (as performance can't be the criteria) I would prefer StrictMath, as its results are reproductible..

For speed consider FastMath Maybe Java + JNI and C++. Was faster some time ago, not sure if still the case today as the JVM are now extremely optimized.

Note

The library performance depends also on the JDK See also What's the difference between java.lang.Math and java.lang.StrictMath?

Community
  • 1
  • 1
Hey StackExchange
  • 2,057
  • 3
  • 19
  • 35