Adding Floating-Point Decimals for Fun and Profit

(blog.vero.site)

26 points | by ibobev 1 day ago

5 comments

  • sobriquet9 16 minutes ago
    Besides Python’s fractions, there are also constructive reals, exemplified by Android calculator where you can swipe any answer to get as many digits as you want.
  • sieve 40 minutes ago
    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.

  • krige 39 minutes ago
    Possibly the first case I've encountered where image loaded before the text. A grid of 1px rectangles? How unsettling.
  • amelius 1 hour ago
    > Many people know that you shouldn’t do decimal calculations, such as those involving U.S. dollars and cents, with the floating-point numbers in most programming languages. This is because decimal numbers can’t be expressed exactly as such floating-point numbers, so you will encounter rounding errors.

    Why not, rounding to the nearest cent is going to be much less precise for any realistic amount of dollars when using 64 bit floats (the type of float Javascript uses in every browser).

    • sluukkonen 53 minutes ago
      It’s easier just to use a proper decimal type than to remember all of the ways where using floats will bite you in the ass. The behavior is unintuitive even in trivial examples. Code like

        amount_due = 0.10 + 0.20
        amount_paid = 0.30
        
        if amount_paid >= amount_due:
            print("PAID")
        else:
            print("OUTSTANDING")
      
      will print “OUTSTANDING”.
      • adrian_b 39 minutes ago
        True, but those who do not use decimal floating-point numbers do not use such binary floating-point numbers.

        They use binary fixed-point numbers/integers, where such computations are exact, while not having the huge computational overhead of decimal floating-point numbers.

    • glimshe 1 hour ago
      The rounding behavior with true decimals is easy to control and understand across number magnitudes and doesn't suffer from platform specific quirks like floats/doubles.

      The article's images clearly show the rounding error mess your get without decimals.

      • sobriquet9 11 minutes ago
        There's still catastrophic cancellation, where subtracting two large numbers that are very close results in an answer with reduced precision.
      • amelius 1 hour ago
        But the rounding errors are way down in the nano-cents. Not worrying about them is cheaper!
        • andreareina 44 minutes ago
          The decimal library worries about rounding, that's basically free. With floats you need to decide when to use rounding (because you don't want to show a balance of -0.000000086) and it's more difficult to use automatic checks for the same reason.
        • glimshe 56 minutes ago
          If you're calculating your monthly expenses that's okay, but across millions of transactions of arbitrary amounts, something financial systems do regularly, aggregates start not adding up and you don't know where the money went.
    • knorker 57 minutes ago
      "The nearest cent" is already a bad assumption. Should IEEE 754 representation dictate taxes and money splitting?

      You could end up with splitting an account down the middle, and ending up with an extra cent being created out of thin air, or one destroyed. In billions of transactions each day, this could be a problem for balancing books when there is no longer any equality check.

      When not using floats, the rules and checks become more… deterministic, if you don't mind stretching the definition of that word a bit.

  • jgalt212 33 minutes ago
    > Many people know that you shouldn’t do decimal calculations, such as those involving U.S. dollars and cents, with the floating-point numbers in most programming languages. This is because decimal numbers can’t be expressed exactly as such floating-point numbers, so you will encounter rounding errors.

    Decimals or pennies, you still have rounding errors. For example, if your contract is $100 / year. You will get the full $100 if you bill yearly, quarterly, or semi-annually. But if you bill monthly, no matter if you deal in pennies or fractional dollars, the sum total of your invoices for the year will be less than $100.

    • gottheUIblues 11 minutes ago
      Bill 8 months at $8.33 and 4 months at $8.34?