3 ms·
Welcome to BCD numbers, the staple of financial arithmetics since 1950s!
by nine_k 5y ago
Welcome to BCD numbers, the staple of financial arithmetics since 1950s!
- PaulHoule 5y agoDecimal floating point isn't necessarily the same as BCD. For instance you can represent integers as binary, base 3, BCD, or any base and they are still the same integers. That is you can write the same number 75412 in any of those systems. The notation for the number is different but the same numbers exist in all of those systems. Floats on the other hand are a subset of rationals. In the case of binary floats the denominator is 2^N, in the case of decimal floats it is 10^N. In this case it is not just a difference of notation. The number 1/4 exists in both the decimal and binary floats, but 1/10 exists in the decimal floats but not the binary floats. Thus you have the problem 0.1 + 0.2 != 0.3 because (a) none of the numbers involved really exist in the floats, (b) if you ask for 0.1 you get some other number that is close to 0.1 and prints out as 0.1 but, (c) isn't really 0.1 so what should be an equality is an inequality. You could represent the mantissa (the numerator) of decimal floats with base 2 or base 10 which makes a difference in coding efficiency, how fast calculations are, how fast decimal input/output are, etc. It's the base 2 vs base 10 exponent that makes binary floats appear defective.
- Dylan16807 5y ago> For instance you can represent integers as binary, base 3, BCD, or any base and they are still the same integers. And in particular, decimal floating point usually encodes numbers as either fully binary, or in batches of 10 binary bits that hold 3 digits each.