3

I am trying to get exact same results on 2 different platform i.e. Solaris and Linux(sun-studio vs GCC). The entire code uses double datatype. But when i print the output i see differences in the floating point(like in the 20th decimal place).

What i need to know is whether i can set some compiler flag to make both GCC and sun-studio compiler behave equally. Attached Image shows difference in Double datatype. Left side is the Output from GCC and other one is from sun-studio.

enter image description here

  • There are no such flags and there is nothing you can do about this except learn to live with it. – molbdnilo Jan 14 '20 at 09:05
  • 1
    If you are concerned about this, you might have a design issue. – mfnx Jan 14 '20 at 09:06
  • You cannot force two compilers to produce the same code, as they are different products. However, you can probably improve the accuracy by using `long double` instead. – VLL Jan 14 '20 at 09:07
  • Also, this is affected not only by the compiler, but also by the OS and hardware. – mfnx Jan 14 '20 at 09:07
  • Floating point data is always dynamic and when using it in code you should never compare anything to an exactly value as its highly unlikely that the floating point value will ever be the exact value you expect, instead compare with a tolerance. – SPlatten Jan 14 '20 at 09:15
  • @vll long double might improve the accuracy, but they're going to be even less the same same as sun studio will interpret that as 128 bit type - as opposed to gcc 80 bit at least on x86. – camelccc Jan 14 '20 at 10:24
  • 1
    Maybe you get the same result, but the printing method differs. You print numbers with more significant digits than the double datatype usually have, so the print routine may differ how it prints the extra digits. If both platform uses IEEE754, and you just use basic operations (+, -, *, /, sqrt), then you should get the same result. If you use other operations, there are open source libraries which calculates other operations with basic operations, so you can make your program consistent across platforms. – geza Jan 14 '20 at 12:20
  • IEEE 754 provides guidelines, but does not guarantee that the results of operations are reproducible. – Alex Reynolds Jul 07 '21 at 18:47

1 Answers1

9

The main issue comes from this :

I am trying to get exact same results on 2 different platform

You are not guaranteed that you will have the same results on two different platforms. They may differ on so many ways on how they implement floating operations.

One solution to your problem: fixed-point arithmetic.

Also, take a look at these answers :

Cross Platform Floating Point Consistency

Are IEEE float and double guaranteed to be the same size on any OS?

They are a quite good start for you to understanding what is going on.

luizinho
  • 126
  • 1
  • 5
  • 3
    Further, you're not even guaranteed the same result on the _same_ platform, if you change the order of (notionally commutative) operations, etc. – Useless Jan 14 '20 at 09:50
  • @Useless: on the other hand, if a platform uses IEEE-754 (which very common today), consistency is guaranteed for +, -, *, / (these are commutative as well) and sqrt. – geza Jan 14 '20 at 12:32
  • @geza -- consistency is guaranteed to the extent that the compiler follows the rules. Most compilers by default **don't** follow the rules, in order to produce code that runs faster. They will have a command-line switch to turn on full conformance. – Pete Becker Jan 14 '20 at 15:31
  • @PeteBecker: gcc, clang, msvc and icc do follow the rules by default. Which compilers are you referring to? – geza Jan 14 '20 at 18:47
  • @geza -- well, all of them. Maybe things have changed from a few years ago. The IEEE requirement that's most ignored is that storing a calculated value narrows its type to the type of the stored value. That narrowing can be expensive (cf. Java's disastrous "strict" fp), and when the store was just to an intermediate variable and the value of that variable was used in subsequent calculations, the compilers ignore the store and continue to do the calculation at full internal precision. – Pete Becker Jan 14 '20 at 20:03
  • @PeteBecker: ah, ok, that's an exception. i387 based code. I forgot about it, it was a long time ago. There was a solution for this, though, like GCC's `-ffloat-store`. But you're right, this is not enabled by default. – geza Jan 14 '20 at 20:14