Decimal arithmetic is good for money. Decimal fixed point is good for money. Decimal floating point may not be.
Dividing 7 pennies by 4 is a good example. 7/4 = 1 3/4, so the 3/4 is accurately expressed in d fractional digits where base^d mod 4 == 0. For base-10 decimal arithmetic, this means two digits; 4 divides 100 but not 10. So 1.75 is exact. Dividing 7 pennies by 5, 7/5 = 1 2/5, needs just one fractional digit since 5 divides 10. Dividing 7 pennies by 3, however, is the important case. 7/3 = 2 1/3. 3 does not divide any power of ten, so this is an unrepresentable fraction in base 10 and leads to the nonterminating expression 2.3333333333... How many threes you keep is thus a determination of how accurately the computed values match the mathematical value. Here is the insight that drives understanding: in floating point the number of threes varies based on how big the value being computed happens to be (7/3 is more precise than 10/3); in fixed point decimal the number of threes is a specified quantity that is known and can be reasoned about. My opinion is that if you study financial accounting standards and regulations, you will discover that choosing a known fractional accuracy for money values is recognized practice. On Sun, Sep 2, 2018 at 9:58 AM Manlio Perillo <manlio.peri...@gmail.com> wrote: > On Sunday, September 2, 2018 at 5:16:16 PM UTC+2, ohir wrote: >> >> On Sun, 2 Sep 2018 07:23:55 -0700 (PDT) >> Manlio Perillo <manlio....@gmail.com> wrote: >> >> > What happens if you need to divide 7 pennies by 4? >> >> I do not know about pennies peculiarities, so I'll stay with ¤ ;) >> >> I divide 70000µ¤ by 4 then It gives 17500µ¤. On customer account it >> still is 17500µ¤ but on his statement it prints 0.018¤ (using 5/4 >> rounding >> rules). The -500µ¤ goes to wherever local accounting law imposes on me. >> >> Surely with pennies I'd do same thing ;) >> >> > For financial applications you should use decimal floating-point >> numbers. >> And soon see pink slip. Or even go to jail. >> >> > Then can I assume that all people using the Python decimal module are > going to jail? > > According to https://en.wikipedia.org/wiki/Floating-point_arithmetic, and > to the decimal module documentation, Decimal is implemented using decimal > floating point arithmetic. > https://github.com/python/cpython/blob/master/Lib/_pydecimal.py#L509 > > > Manlio > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- *Michael T. jonesmichael.jo...@gmail.com <michael.jo...@gmail.com>* -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.