decimal32 vs float32, which is better for storage

1.9k views Asked by At

According to IEEE 754-2008, there are binary32 and decimal32 standard:

                                                            Decimal Decimal
Name        Common name         Base  Digits E min  E max   Digits  E max
binary32    Single precision    2     23+1   −126   +127    7.22    38.23
decimal32                       10    7      −95    +96     7       96

So both use 32 bit but decimal 32 has 7 digit with E max as 96 while float32 has 7.22 digit and E max is ~38.

Does this mean decimal 32 has similar precision but far better range? So what prevents using decimal32 over float32? Is that their performance (ie.speed)?

3

There are 3 answers

0
Patricia Shanahan On

If you need exact representation of decimal fractions, use decimal32. If generally good approximation to arbitrary real numbers is more important, use binary32.

1
Pascal Cuoq On

Your reasoning when you say “decimal 32 has similar precision …” is flawed: between 1 and 1e7, binary32 can represent far more numbers than decimal32. Choosing to compare the precision expressed as an “equivalent” number decimal digits of a binary format gives the wrong impression, because over these sequences of decimal digits, in some areas, the binary format can represent numbers with additional precision.

The number of binary32 numbers between 1 and 1e7 can be computed by subtracting their binary representations as if they were integers. The number of decimal32 numbers in the same range is 7 decades(*), or 7e7 (1e7 numbers between 1 and 9.999999, another 1e7 numbers between 10 and 99.99999, …).

(*) like a binade but for powers of ten.

0
calandoa On

Decimal32 does not have similar precision than float. For instance, let's take 1 000 000 000 (1 billion).

With Decimal32, it will be represented in internal memory as 1 000 000×103, with 7 digit for the significand. There is actually many other representation possible for 1 billion (100 000×104, ... all of them being called the cohort of this number), but they would be less precise for what we will try to do.

In case of the float, it is represented as 15 625 000×26 or more exactly in binary as 1.11011100110101100101000×10(10011100-1111111).

Therefore, the smallest step here to the next exact number (i.e. by adding 1 to the LSB of the significand) is for Decimal32 103 = 1000 but 26 = 64 for float, so the float is ×15.625 more precise in this case! Note however that this is not the case for all numbers; the ratio will depends on both exponents, and I choosed here a specific case with an important difference.

This reduced precision for Decimal32 is the price to pay for their extended range.