Jump to content
  • Advertisement
Sign in to follow this  
Fred304

[java] determinism

This topic is 4240 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Are floating point operations guaranteed to yield the same result on different machines? For now I calculate the square root of an integer using double Math.sqrt(double). Is it safer to write my own integer square root finder?

Share this post


Link to post
Share on other sites
Advertisement
The same code may yield different results on the same machine (so it might do so on different machines as well). x386 SSE2 floating-point registers are 80-bit, but memory storage is 64-bit. Therefore, the exact same code may produce different results depending on whether the registers were flushed or not in-between computations. If Java atempts to standardize the behaviour of floating-point numbers, it will do so at the cost of performance (since operations would have to be emulated in software), so I do not believe it would happen.

Share this post


Link to post
Share on other sites
You may be interested in StrictMath.

Java does make certain gurantees about accuracy (which does involve some additional work over just letting the CPU do it) so results should be accurate to a certain number of digits. However they're not going to be absolutely identical, you'll have to go poking through the JVM spec for this info.

Share this post


Link to post
Share on other sites
Just to be on the safe side I implemented my own version of sqrt(int). It turned out to be a lot slower than Math.sqrt(double), but I'm only calling it once every few seconds, so that should not matter.

public static int sqrt(int x)
{
int guess = INITIAL_GUESS[x >> 23];
int next = (guess + x / guess) >> 1;
do
{
guess = next;
next = (guess + x / guess) >> 1;
}
while (next < guess);
return guess;
}

private static final int[] INITIAL_GUESS = { 2048, 3547, 4579, 5418, 6144,
6792, 7384, 7931, 8444, 8927, 9385, 9821, 10240, 10641, 11028,
11402, 11764, 12116, 12457, 12789, 13113, 13429, 13738, 14040,
14336, 14625, 14909, 15188, 15462, 15730, 15995, 16255, 16511,
16763, 17011, 17256, 17498, 17736, 17971, 18203, 18432, 18658,
18881, 19102, 19320, 19536, 19750, 19961, 20170, 20377, 20582,
20784, 20985, 21184, 21381, 21577, 21770, 21962, 22152, 22341,
22528, 22713, 22897, 23079, 23260, 23440, 23618, 23795, 23971,
24145, 24318, 24490, 24661, 24830, 24999, 25166, 25332, 25497,
25661, 25824, 25986, 26147, 26307, 26465, 26624, 26781, 26937,
27092, 27246, 27400, 27553, 27704, 27855, 28005, 28155, 28303,
28451, 28598, 28745, 28890, 29035, 29179, 29322, 29465, 29607,
29748, 29889, 30029, 30168, 30307, 30445, 30583, 30720, 30856,
30991, 31126, 31261, 31395, 31528, 31661, 31793, 31925, 32056,
32186, 32316, 32446, 32575, 32703, 32831, 32959, 33086, 33212,
33339, 33464, 33589, 33714, 33838, 33962, 34085, 34208, 34330,
34452, 34574, 34695, 34816, 34936, 35056, 35175, 35294, 35413,
35531, 35649, 35766, 35883, 36000, 36116, 36232, 36348, 36463,
36578, 36692, 36807, 36920, 37034, 37147, 37260, 37372, 37484,
37596, 37707, 37818, 37929, 38039, 38150, 38259, 38369, 38478,
38587, 38695, 38804, 38912, 39019, 39126, 39234, 39340, 39447,
39553, 39659, 39764, 39870, 39975, 40080, 40184, 40288, 40392,
40496, 40600, 40703, 40806, 40908, 41011, 41113, 41215, 41316,
41418, 41519, 41620, 41720, 41821, 41921, 42021, 42121, 42220,
42319, 42418, 42517, 42616, 42714, 42812, 42910, 43008, 43105,
43202, 43299, 43396, 43492, 43589, 43685, 43781, 43876, 43972,
44067, 44162, 44257, 44352, 44446, 44541, 44635, 44728, 44822,
44916, 45009, 45102, 45195, 45288, 45380, 45472, 45565, 45657,
45748, 45840, 45931, 46023, 46114, 46204, 46295 };


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!