Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: what did I just see..?

by BillKSmith (Monsignor)
on Mar 21, 2021 at 21:58 UTC ( #11130057=note: print w/replies, xml ) Need Help??


in reply to what did I just see..?

In practice, floating-point errors are seldom a problem. Look at your output carefully and see how small the error really is. The default print format can be annoying (as you have discovered). You can avoid this by using printf to round to your required precision.
Bill

Replies are listed 'Best First'.
Re^2: what did I just see..?
by LanX (Sage) on Mar 22, 2021 at 01:06 UTC
    > In practice, floating-point errors are seldom a problem

    Sorry, I must disagree.

    I know of a case where fiscal authorities rejected a calculation because it was 1 cent off. Which led to a missed deadline and penalty payments, IIRC.

    If you need cent accuracy then calculate in cents. Never floats!

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    PS: And if you do accounting, inform yourself about the required precision and rounding rules.

      I got a threatening letter from American Express for my Amazon.com corporate card—which I hadn’t used in a full year—telling me to pay up on my outstanding balance or else! The amount I owed: $0.00. :P

        I knew a man with the same problem. He solved it with a check for $0.00. I do not think it was cashed.
        Bill
        Scandalous!

        I hope you learned the lesson and payed your debts.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        I now have a brand new understanding of the term "nickel and dime-ing" ;)

      All your suggestions are true. My word 'seldom' probably does not apply to financial calculations. It may not be possible to exactly duplicate the calculations on the statement you receive from your financial institution. The printed decimal numbers on that statement are not exactly the same as the binary numbers in the bank's computer. It simply is not possible to recover every bit of those numbers. Any calculation that you do is flawed to start with. I have noticed that even Quicken's calculation of price/share never exactly agrees with my statement.
      Bill
        Financial calculations can be quite hairy AFAIK,

        And for very good reasons.

        They have special rounding rules and fixed precision like a tenth or hundreth of a cent.

        Like

        1 Euro = 1.95583 Deutsch Mark

        But it's all integer in nature.

        And bank rules may not be fiscal rules.

        The only way I can think of to handle such systems transparently in Perl is using objects with overloaded operators. (With internal integers)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130057]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2022-05-22 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (80 votes). Check out past polls.

    Notices?