0

Having issues with the sqrt function in two languages.

I have a JAVA API and C++ client, I'm trying to use the sqrt functions in both but they give me slightly different numbers.

The inputs are:

x = 25.0
y = 5625.0

Java:

double distance = Math.sqrt(x + y); 
// outputs 75.16648189186454

C++:

const double distance = std::sqrt(x + y);
// outputs 75.166481891864535

I need the numbers to be the same as I'm using them as seeds in the API and client. Is there any way to do this? Ideally the java output 75.16648189186454, however, I will take either.

Many thanks

Andy
  • 3,794
  • 24
  • 28
VortexFX
  • 127
  • 7
  • I don't know how you are getting the square root of (5 + 75) being 75.166. I get something around 8. Are you using different code/values? – Water Aug 18 '19 at 15:02
  • 2
    Using the result of float computations for this sounds like a real bad idea to begin with. – Mat Aug 18 '19 at 15:03
  • 2
    They're the same number, just being displayed with slightly different digits of precision. – Shawn Aug 18 '19 at 15:04
  • @Water, yes thanks I have corrected the mistake – VortexFX Aug 18 '19 at 16:51
  • @Mat, perhaps but its what I'm doing and I have been following a thesis on procedurally generated galaxies. – VortexFX Aug 18 '19 at 16:54
  • @Shawn, yes I am aware that they are the same number in different precisions, I'm just wondering if it's possible to alter the precision in one or both of the languages. – VortexFX Aug 18 '19 at 16:55
  • @VortexFX: if you need the same results on multiple languages & platforms, you are probably better of using a fixed point library. This may help: https://gamedev.stackexchange.com/questions/174320/how-can-i-perform-a-deterministic-physics-simulation – Mat Aug 18 '19 at 17:55
  • @VortexFX Are none of the answers satisfactory for you? – Water Sep 03 '19 at 03:53

3 Answers3

1

When I get look at the bits from both C++ and Java, they result in:

Java:

4634989787871853517

C++:

4634989787871853517

Which means they are both the same bits. Since they should be following IEEE-754, this means both languages have an identical value. You just see one output be slightly truncated in one language, but the value is not.

Water
  • 3,245
  • 3
  • 28
  • 58
0

Floating point numbers are not exact and you cannot depend on different implementations (languages) getting the exact same value. Nor can you rely on the same language getting the same value on different hardware.

Serialising floating point numbers and transmitting them between different languages and/or hardware implentations is a hard (not N/P hard, but still really difficult) problem.

I'd recommend reading these links for in-depth details:

What Every Computer Scientist Should Know About Floating-Point Arithmetic

Is floating point math broken?

Sometimes Floating Point Math is Perfect

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
0

To expand on Shawn's comment: The C++ reply has 17 digits, the java reply has 16. If you round the 17 digits you will get the same result, as 35 rounds to 4. Double has in fact slightly less than 16 (approximately 52+1 bits times log 2) meaningful digits, so the C++ result is misleadingly precise. You can control the number of digits displayed both in C++ and in Java, but as Shawn said the actual number in the bowels of the computer is the same.

Jonathan Rosenne
  • 2,159
  • 17
  • 27