4

Firstly i am aware that different hardware architectures handle floats differently, so I can't guarantee the same result on different machines even with the same inputs.

But in my case i do not have a compounding issue where the differences add up over time (like you might experience with physics engines), so i can forgive differences providing the difference is small enough that different machines will still be in sync as far as a user could tell.

So i wondered just how significant of a difference can floats be between different hardware for math operations?

If its tiny like < 0.001 then i can live with that, but if its quite a large difference then i might have no choice but to use fixed point calculations but i want to avoid that if i can for performance reasons.

I don't know where to find a clear answer on this that was easy to understand as i am just a self taught programmer not a CS graduate.

I am developing a game so in my case the only platforms that matter are consoles and computers - not so much tablets & phones. Hope some one knows some info on this topic here!

1
  • If you're sticking to mainstream CPU architectures like x64 (Intel, AMD) or ARM, then floats will behave as expected. In contrast, many embedded chips don't support floats. But the precision of floats depends on the size of the value – if you need to have the same precision over the entire domain, fixed point representation (i.e. using ints, or snapping everything to a grid) is more appropriate.
    – amon
    Jun 27, 2020 at 6:08

2 Answers 2

4

Most modern processors are compliant with IEEE 754 floating point representation. Most processors at least support 32 bit single precision format. Internally i.e., within the processors, they use a even higher bit size representation (extended format) for intermediate results. Extended representation is 44 bit for 32 bit floats and 80 bits for 64 bit double.
Put it simply, 32 bit representation provides at least 7 digit of precision after decimal and double 64 bit provides close to 16 digits of precision after decimal.
The offered precision is way more than our expectation (0.001).

So, use built-in float and double data types without any worry.

9
  • Awesome. thanks for the information !
    – WDUK
    Jun 27, 2020 at 6:29
  • 2
    One thing that you should be aware of is that rounding of decimals might yield unexpected results because decimals can't be represented exactly with binary floats. Search the internet for articles on floating point rounding, there are just too many to mention a single one here. Jun 27, 2020 at 6:39
  • Is rounding not consistent using the same IEEE 754 representation?
    – WDUK
    Jun 27, 2020 at 6:52
  • 2
    The IEEE 754 define five different rounding rules. While the result may vary on different processors but different in magnitudes on different processors will still be less than 0.0000001.
    – ajit
    Jun 27, 2020 at 7:01
  • 1
    @WDUK, rounding of floats is fairly consistent, but it might not match your expectations when you try to re-do the math by hand. That is what trips most people up. And rounding is happening at two places: During the calculations to fit the results in the fixed-width storage and when displaying the result. That can result in output like "1.0 does not equal 1.0", because the internal representations are different, but they both round to 1.0 on output. Jun 27, 2020 at 7:49
2

Practically everyone uses the IEEE 754 standard or something very close to it. There may be exceptions for DSPs, I’ve never written code for any of them. You might find a VAX which might have a different floating point format. But since 1981 you can practically assume ieee 754 format.

There are two variations: Bigendian and littleendian, same as for integers. In practice, POWER is bigendian, everything else is littleendian. Other than for data exchange, it doesn’t matter.

There are five formats supported by the standard: 16, 32, 64, 80 and 128 bit. All current processors support 32 and 64 bit, many support 80 bits, some support 16 bit, and none I know support 128 bits (corrections welcome). Some PowerPC compilers support a 128 bit format that is just the sum of two 64 bit double precision numbers, giving an effective mantissa of 103 bit, not ieee 754 compatible.

Some processors support Ieee 754 without denormalised numbers. Intel processors support mantissa from the 64 bit format combined with the 80 bit exponent range. And some support calculating a x b + c with a single operation and a single rounding error.

3
  • 1
    Why do so many computer games that require deterministic results still opt for fixed point mathematics for their physics engines if they can safely say there will not be divergence after some frames have past due to most platforms adhering to the same standard?
    – WDUK
    Jun 28, 2020 at 4:40
  • @WDUK Because (1) games typically use more than just the basic arithmetic operations governed by IEEE-754, for example various math functions taken from a compiler or platform library (2) popular high-level languages offer only imperfect bindings to IEEE-754 arithmetic, or none at all
    – njuffa
    Jul 15, 2020 at 19:19
  • "... or something very close to it." - Yes. Complete IEEE 754 adherence is a goal, rarely met. Jul 23, 2020 at 2:09

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.