Floating point: Can I recursively divide/multiple numbers by 2 and never get rounding errors?

血红的双手。 提交于 2019-12-11 14:26:49

问题


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?


回答1:


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.)



来源:https://stackoverflow.com/questions/47496094/floating-point-can-i-recursively-divide-multiple-numbers-by-2-and-never-get-rou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!