Puneet Varma (Editor)

Dec64

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

DEC64 is a proposed format for storing integer and decimal numbers in a computer. Its aim is to solve some drawbacks of the very widely used format described by IEEE 754. Namely, in languages which implement IEEE 754, some very simple arithmetic operations can have unexpected results due to rounding errors. DEC64 was proposed by Douglas Crockford.

Contents

Example

In Javascript (which implements IEEE 754), the expression 0.1 + 0.2 will have a result which is not exactly 0.3:

DEC64 attempts to solve such cases while maintaining desirable properties of IEEE 754.

Representation

DEC64 represents numbers as 64 bit values composed of two parts:

  • the least-significant 8 bits contain the exponent;
  • the remaining 56 bits contain the coefficient.
  • Both parts are two's complements, such that the exponent will be between -127 and 127, and the coefficient will be between -36028797018963968 and 36028797018963967.

    The value of a number is given by:

    value = coefficient * 10exponent

    There are 255 possible representations of zero. They are all considered to be equal.

    There is a special value called NaN ("not a number") that has a coefficient of 0 and an exponent of -128. The result of division by zero is nan. nan is also the result of operations that produce results that are too large to be represented. nan is equal to itself.

    When an arithmetic operation has an input with an exponent of -128, the result will be nan.

    Integers can have an exponent of 0 as long as the coefficient is less than 36 quadrillion. One interesting property of DEC64 is that an integer number can be easily converted to DEC64 simply by shifting it left 8 bits.

    References

    Dec64 Wikipedia