5 ms·
floating point error. BCD guarantees you that 1/10th, 1/100th, 1/100th, etc (to some configurable level) will be perfectly accurate, without accumulating error
by finnh 4y ago
floating point error. BCD guarantees you that 1/10th, 1/100th, 1/100th, etc (to some configurable level) will be perfectly accurate, without accumulating error during repeat calculations.
floating point cannot do that, its precision is based on powers of 2 (1/2, 1/4, 1/8, and so on). For small values (in the range 0-1), there are _so many_ values represented that the powers of 2 map pretty tightly to the powers of 10. But as you repeat calculations, or get into larger values (say, in the range 1,000,000 - 1,000,001), the floating points become more sparse and errors crop up even easier.
For example, using 32 bit floating point values, each consecutive floating point in the range 1,000,000 - 1,000,001 is 0.0625 away from the next.
jshell> Math.ulp((float)1_000_000)
$5 ==> 0.0625
- elpocko 4y agoYou can have infinite precision in pretty much any accurate representation though, no? Where is the advantage in using BCD over any other fixed point representation?
- danbruc 4y agoYou are confusing two things. Usually you represent decimal numbers as rational fractions p/q with two integers. If you fix q, you get a fixed point format, if you allow q to vary, you get a floating point format. Unless you are representing rational numbers you usually limit the possible values of q, usually either powers of two or ten. Powers of two will give you your familiar floating point numbers but there are also base ten floating point numbers, for example currency data types. BCD is a completely different thing, instead of tightly encoding an integer you encode it digit by digit wasting some fraction of a bit each time but make conversion to and from decimal numbers much easier. But there is no advantage compared to a base ten fixed or floating point representation when it comes to representable numbers.
- elpocko 4y agoThis was one of those things where I know just enough to realize something about the reasoning is not right. Thank you for putting that feeling into competent words.
- jasomill 4y agoAs a practical example, POWER architecture uses the densely-packed decimal encoding[1] to encode decimal digits within its IEEE 754-compliant decimal floating-point format[2]. IEEE 754 also supports encoding decimal integers as binary, by converting the entire decimal integer to a single binary integer (i.e., not by storing each decimal digit as a separate binary number). [1] https://en.wikipedia.org/wiki/Densely_packed_decimal https://en.wikipedia.org/wiki/Densely_packed_decimal [2] https://files.openpower.foundation/s/dAYSdGzTfW4j2r2#page=221 https://files.openpower.foundation/s/dAYSdGzTfW4j2r2#page=22...
- ajross 4y agoAs others are pointing out, decimal fidelity and "error" are different things. Any fixed point mantissa representation in any base has a minimal precision of one unit in its last place, the question is just which numbers are exactly representable and which results have only inexact representations that can accumulate error. BCD is attractive to human beings programming computers to duplicate algorithms (generally financial ones) intended for other human beings to execute using arabic numerals. But it's not any more "accurate" (per transistor, it's actually less accurate due to the overhead).
- deleted 4y ago[deleted]