2

I'm concerned about floating-point determinism and the fact that different CPUs have different implementations of FPU operations such that the same C++ code will have different results when compiled and run on different machines.

How can I test my code on other platforms (i.e CPUs) without having to physically buy new hardware myself?

  • Only way to be 100% sure is to buy the hardware. Emulators (if they exist) may not implement the exact FP implementation that the hardware does. – CHendrix Mar 16 '17 at 19:00
  • 4
    You should never assume FPUs to behave deterministic. For example: There are platforms where floating point accuracy in a CPU register is different than in RAM, and moving from one to the other truncates it. Which number to store where and when is something which is completely in the hand of the compiler. If you need completely deterministic behavior, always use integer arithmetic. – Philipp Mar 16 '17 at 19:06
1

Thinking that floating-point results should always produce the same result is to some degree misguided.

Most implementations today use 32 bit single precision and 64 bit double precision IEEE 754 floating point arithmetic. You could use long double which is 64 bit, 80 bit or 128 bit depending on the implementation, so that's not going to give identical results. But it gives better results. So you may have a choice between better and identical results. What do you prefer, better or identical? I prefer "better".

Your compiler may make fused multiply-add available or not. If available, it gives you more precision, more speed, and different results. What do you prefer, faster and better, or identical? I prefer "faster and better".

If your results on two implementations are slightly different, that's no problem. If they are massively different, you are lucky (kind of): You just found out that there's a problem with your calculations. If you got results A and B, and they are different, and you have no reason why A or B would be the better result and the other would be wrong, then you now know that your calculations just produce bad results and need to be fixed. With identical results, you would never have known.

You can count yourself lucky if you think that having bad results and knowing it is better than having bad results and not knowing.

| improve this answer | |
1

If you want deterministic floating point behaviour, compile the code always on the same machine and run the pre-compiled binaries on whatever machine the software needs to be run. Different compiler optimizations and different compiler versions may slightly adjust the accuracy of floating point calculations.

IEEE 754 should guarantee that if you have the same assembly code, and a different CPU, you will nevertheless get the same results. So, it doesn't really matter where you run the code; it matters where you compile the code (assuming software architecture does not change, which it usually doesn't these days as nearly everything is amd64).

If you want to support ARM architectures, be prepared that the code may produce slightly different answers than when run on amd64.

If you absolutely need determinism, consider using integers. Decimal numbers can be represented by fixed point integers.

| improve this answer | |

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.