‹ BackHN Continuity

Thread

Adding Floating-Point Decimals for Fun and Profit

62 points · 35 comments · ibobev

  1. sieve · · focus · HN ↗
    Accounting has the concept of materiality. But you still want the figures to tally to the last cent.

    One way to do this is to separate the computation/storage and presentation layer: use integers to store values as cents for computation/storage and only convert to dollars+cents on display. But then someone might suddenly demand three digits after the decimal point and you cannot go about changing every stored value just because the logic changed in one part of the application. So, using decimals is the safer choice.

    I prefer integers for my plain text ledger software though because the format is in my control.

    1. adrian_b · · focus · HN ↗
      Using 64-bit integers as a count of a hundredth or of a thousandth part of a cent should be enough for most purposes (i.e. up to ten thousand or one hundred thousand billions of $).

      If you want to be able to count trillions of trillions of dollars with a resolution of a millionth part of a cent, then you can use 128-bit integers.

      Computing exactly with 128-bit integers is many times faster than computing only approximately with decimal floating-point numbers and it requires less memory storage for the same dynamic range.

      1. fourier54 · · focus · HN ↗
        Actually 128 int uses 2 times the storage for less dynamic range. I agree however that for financial applications int is always better. You don't want float dynamic range on finance calculations
        1. adrian_b · · focus · HN ↗
          You are right, I forgot that nowadays standard decimal floating-point numbers do not use BCD encoding any more, but they pack 3 decimal digits into each 10 bits.

          This means that the 64-bit decimal64 format still provides an acceptable precision of 16 digits, even if it is lower than for 64-bit integers, which provide over 18 digits.

          If the goal is to avoid rounding, 64-bit binary integers are still superior to decimal64, due to the availability of more significant digits, but due to its scale factor decimal64 is superior if you want to express a huge amount of money while allowing rounding, which would probably be needed only for amounts greater than the budgets of all countries together.

          You are right that decimal64 provides a greater dynamic range than 128-bit binary integers, but even so the dynamic range of 128-bit integers is many orders of magnitude greater than needed for any imaginable amount of money, while providing exact operations performed at a speed many times greater than for decimal64.

          On computers without hardware support for the IEEE standard decimal formats, much faster operations with decimal floating-point numbers can be implemented if they are not represented like in the standard, but as a product between a binary integer and an integer power of ten.

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.