Context: I'm trying to port into c++ the implementation of a hash function that uses double arithmetic from its original implementation in JavaScript. In this function, the doubles we are operating on can be very small and very large, and any differences would be magnified ( on purpose, since this is a hash function ).

More Details

  • What's going wrong: the hash appears to be producing different results in the cpp version compared to the JavaScript version. What system are you using: currently I am running this on Debian using node 6.10.2 ( for JavaScript ) and g++ (Debian 4.9.2-10) 4.9.2 What have I tried: running the hash on a set of test vectors. What was the result: the hashes of the test vectors are different in the JS and C++ versions. What I want: to understand the cause so I can bring the versions into compatibility or understand that they cannot be compatible. More specifics: In JS I am using Float64Array for the doubles and in C++ I am using double type.

Question: is double arithmetic different on JS and C++ ?

  • 2
    google.com/search?q=IEEE754 Jun 11, 2017 at 5:02
  • Thanks for the link
    – authorized
    Jun 11, 2017 at 5:03
  • 3
    There is no simple answer for C++. While IEEE754 is what everyone assumes and what you get most of the time there are cases that floating point calculations are not using the double precision IEEE754 calculations. Examples for this are -Ofast with gcc or x87 extended precision calculations.
    – moggi
    Jun 11, 2017 at 5:23
  • 1
    Whilst it is true that float calculations are non-portable, it's typically corner-cases that are the problem. However, I worry about hashes in conjunction with floating point. A hash will differ a lot even if just one bit is different, so saying the hash is different is not really a good indication of "the results are different" (because floating point calculations are allowed certain error margins). Compare the actual output values instead. [I assume that the hash is calculated on the result, and it's not a calculation of a hash in float, as that would be rather bizarre] Jun 11, 2017 at 6:44
  • 1
    Just read the question again, and doing a hash calculation in floating point is unlikely to work well on any platform - unless extreme care is taken to avoid rounding errors or such. Find another solution! Jun 11, 2017 at 6:46

1 Answer 1


After considering the comments above, and thinking about this some more, I've come to the conclusions that floating point calculations can:

  • differ across different languages, architectures and implementations,
  • even if all those targets share the same IEEE754 standard.

So the answer is -- aside from doing some serious investigation and handling of ( probably a large number of target-specific ) edge cases, there is no other way to bring the results of floating point ( and especially the differences magnified by a hash functions that uses floating point ) into any kind of coherent agreement across targets.

In this case, it turns out that is not such a terrible thing, and may even have some advantages, such as being able to fingerprint particular targets based on hashes.

Anyway, thanks for your efforts. I'd upvote you if I could, cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.