0

Can I always trust that when I divide two IEEE 754 doubles that exactly represent integers, I get the exact value?

For example:

80.0/4.0
=> 20.0

The result is exactly 20.0.

Are there some potential values where the result is inaccurate like 19.99999999?

XrXr
  • 2,027
  • 1
  • 14
  • 20
  • ... is your question: Can I always trust that when I divide two IEEE 754 doubles that represent integers the result is exact? Because your question you have written is trivially false. Integers are unbounded so just take a sufficiently big even integer `a` and divide it by `2` and you end up that its representation as float is `+infinity` and you can get an incorrect result for `a / b`. – Bakuriu Sep 29 '15 at 17:47
  • first and foremost you assume that the int to float conversion was accurate/correct. you are even assuming that fixed or float the compiler converted with the precision you hoped for to the binary form for the program. have seen all of these things fail on what would normally be expected to just work. then yes as Kevin said below, so long as the mantissas dont push off the end the math at that point is integer math and if it results in an integer that after normalizing doesnt fall off the end of the mantissa, then it is accurate. – old_timer Sep 29 '15 at 17:58
  • an old hp calculator I think it was would divide 15/3 and get 4.99999 or 15/5 and get 2.99999 or something like that wasnt hard to trip it up. Also in the days of the famous pentium bug microsofts calculator had a similar bug that wasnt related to the pentium bug. my memory is getting a little rusty on those details. bottom line if you want accurate integers stop using floating point. if you want good enough and dont want to deal with high precision then use floating point. – old_timer Sep 29 '15 at 18:00
  • @Bakuriu You are correct. Edited my question. Thanks for catching that :) – XrXr Sep 29 '15 at 18:17
  • possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Pankaj Saboo Sep 30 '15 at 01:36

2 Answers2

5

According to Wikipedia,

[floating point] division is accomplished by subtracting the divisor's exponent from the dividend's exponent, and dividing the dividend's significand by the divisor's significand.

If one number evenly divides another, then the former's significand should evenly divide the latter's significand. This will give you an exact result.

Kevin
  • 74,910
  • 12
  • 133
  • 166
5

If both integers can be exactly represented by the floating point format (i.e. for doubles, they are less than 253), then yes you will get the exact value.

Floating point is not some crazy fuzzy random algorithm, it does have well-defined deterministic behaviour. Operations that are exact will always give exact answers, and operations that are not exact will round to the representable value (assuming you haven't done anything funny with your rounding modes): "problems" arise when (1) the inputs are not exactly representable (such as 0.1, or 1020), or (2) you're performing multiple operations, some of which may cause intermediate rounding.

Simon Byrne
  • 7,694
  • 1
  • 26
  • 50