in reply to Re: Decimal Arithmetic
in thread Decimal Arithmetic

And no library is going to give you exact calculations

Why not? If you're just going to use the basic operators (+-/*%), then you can store any number precisely as a fraction of arbitrarily large integers.

Replies are listed 'Best First'.
Re^3: Decimal Arithmetic
by liverpole (Monsignor) on Feb 05, 2007 at 23:20 UTC
        you can store any number precisely as a fraction of arbitrarily large integers

    Okay, how would you store pi precisely as a fraction of arbitrarily large integers?  How about the square root of 2?  Or e?

    My point is, unless you're using unlimited storage, there's always a potential for round-off to occur.  It may be very, very miniscule round-off, and it may not affect monetary calculations, but you've always got the potential for introducing such errors, depending on what operations you're performing.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

      I don't remember the last time I owed π dollars to someone. Remember, you are starting with finite amounts (fractions of cents), not numbers like π.

      There not always a potential for round-off to occur, when the system has bounds. When something is 1/3rd off, the discount will be rounded/ceiled/floored. Infinite precision is not needed.

            "Infinite precision is not needed"

        Yes, that's the point I was making.

            "I don't remember the last time I owed π dollars to someone"

        Well, you did say "you can store any number"  :-)

        But even if we do limit the calculations to strictly financial ones, it's very easy to demonstrate a case where you'll get round-off.

        Assume you have invested $1000, and your average annual interest rate is exactly 4%.  According to the The Rule of 72, you can expect your money to double in approximately (72 / 4) = 18 years.

        The problem is, this isn't exact.  The exact period after which your money will double is given by the formula ln 2 / ln 1.04, which is approximately 17.67298769 years.  But my point is, no module is going to give you 100% accuracy for that, because ln 2 and ln 1.04, as well as their quotient, are all transcendental numbers, which means you can't represent them 100% accurately.  Which is all I was trying to get across.


        s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/