1

http://floating-point-gui.de/formats/binary/

binary can only represent those numbers as a finite fraction where the denominator is a power of 2

Does this mean that the numbers calculated by this process can all be added to each other or multiplied by 2 any number of times and still have an exact binary/floating point representation with no rounding errors?

const dv2 = (num, limit) => {
    limit--;
    if (limit === 0) {
        return;
    } else {
        console.log(num, limit);
        dv2((num / 2), limit)
    }
};

Is it possible to determine when a floating point number is exact or an approximation?

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
zino
  • 1,222
  • 2
  • 17
  • 47
  • what language are you operating in? There is a limit in resolution after which you cannot go smaller (C# float around -3.4 × 1038 bis + 3.4 × 1038) with a precision of 7 digits. – Patrick Artner Nov 26 '17 at 12:32
  • javascript and storing numbers in sqlite as type real (float 8 byte IEEE) – zino Nov 26 '17 at 12:45

1 Answers1

5

Yes, as long as you are using a binary floating-point (such as the very common IEEE-754 binary floating-point) and do not overflow to infinity or underflow to subnormal numbers. In binary floating-point, multiplication or division by two is exact until the exponent limits are reached.

For 32-bit IEEE-754 binary, the finite normal values go up to 2128−2104 and down to 2−126. (There are positive subnormal values as low as 2−149, but they have fewer bits in their significand [fraction part] than normal numbers, so precision is reduced.)

For 64-bit IEEE-754 binary, the finite normal values go up to 21024−2971 and down to 2−1022. (There are positive subnormal values as low as 2−1074.)

There is no way (without context) to determine whether a floating-point number exactly represents or only approximates some prior number. (There is a way to detect when floating-point operations have rounded a result. Accessing this additional information is often neglected in implementations of programming languages.) Once a number is converted to a floating-point number, the floating-point number exactly represents what it represents. It contains no information about the amount of rounding error that has occurred previously. (If you have other information about the prior number, such as that it came from a decimal numeral with five significant digits, then you may be able to deduce things about the original number.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • "possible to determine when a floating point number is exact " --> Does javascript have access to FP flags like _inexact_? – chux - Reinstate Monica Nov 26 '17 at 18:36
  • @chux: I doubt it. “Inexact” does not appear in the ECMA-262 standard, and neither does “underflow”, which I would expect to appear at least once if IEEE-754 floating-point flags were supported. But I edited the answer to mention the possibility. – Eric Postpischil Nov 27 '17 at 00:23