I am wondering whether or not Java uses a uniform precision for floating point numbers (float and double), regardless of the machine architecture that runs the VM. This is required because I trying to synchronize some simulations that involve math on several machines and I must make sure that all of the calculations will produce the same results on all of them.
Asked
Active
Viewed 879 times
3
-
2The specification itself dictates [IEEE-754 1985](http://www.javaworld.com/jw-10-1996/jw-10-hood.html) for the format (which encompasses the precision), but a number of "basic" Math functions *may differ* ([see here for an explanation](http://stackoverflow.com/questions/4232231/whats-the-difference-between-java-lang-math-and-java-lang-strictmath)). Also, I am *not sure* if something like `x/y` (where x and y are arbitrary float/double values) is strictly guaranteed to yield the same identical result everywhere. – user2246674 Sep 06 '13 at 23:32
-
1That is a different issue - x/y will always return the same result across JVMs. But `Math.sin(x)` (for example) might return different results depending on how the method has been implemented. – assylias Sep 06 '13 at 23:40
-
In general the Java spec is quite specific as to the precision of numeric quantities. The "strict" stuff only applies to a few obscure cases that are of more theoretical interest than practical. If you are trying to get exactly the same results on different machines, however, and you are using transcendental functions, you should use StrictMath rather than Math. And you should also use the `strictfp` modifier on your classes (whether using transcendental functions or not). – Hot Licks Sep 06 '13 at 23:42
2 Answers
6
You'll be fine. The precision is specified by the language. It's supposed to be the same on every Java platform. The details are at http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3

Dawood ibn Kareem
- 77,785
- 15
- 98
- 110
-
3Note that implementations of the `Math` class are permitted to go slightly astray, in the interests of performance. Implementations of `StrictMath` must give outputs that are bit-for-bit the same as the standard published algorithms. This is not the same thing as the precision of the numbers themselves. – Dawood ibn Kareem Sep 06 '13 at 23:38
-
Because I figured that out myself and didn't want you spending extra time on such a foolish question. But thanks for the answer! – Vladimir Gazbarov Sep 06 '13 at 23:41
2
Java has IEEE 754 single and double precision types.
In other words yes. It will be uniform through out all machines.
However, calculations might vary. @ user2246674

Community
- 1
- 1

progrenhard
- 2,333
- 2
- 14
- 14