Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^8: Reasons for Using Perl 6

by Anonymous Monk
on Jan 03, 2018 at 19:40 UTC ( #1206623=note: print w/replies, xml ) Need Help??


in reply to Re^7: Reasons for Using Perl 6
in thread Reasons for Using Perl 6

So the Rat type doesn't solve all issues, I agree with that and never made any claim to the opposite, but it would solve many issues.
It doesn't solve any issues, because there's a hole drilled in the bottom of the Rat type. Your ship will eventually sink, it's just a question of when.
> .3 - .2 - .1 0 # good > 3*10**-10 - 2*10**-10 - 1*10**-10 0 # good > 3*10**-20 - 2*10**-20 - 1*10**-20 -1.50463276905253e-36 # oops
Monetary amounts? You will hit this error very fast if you start compounding interest.

Replies are listed 'Best First'.
Re^9: Reasons for Using Perl 6
by Laurent_R (Canon) on Jan 03, 2018 at 23:03 UTC
    On compound interests, as I already said earlier, I would normally use the standard exponential formulas and, of course, end up with floats. For example, with a 1.6% interest rate over 10 years compound annually and an initial value of 10,000:
    > my $start_val = 10_000; 10000 > my $end_val = $start_val * (1 + 0.016)**10 11720.2555035677 > say $end_val.WHAT (Num)
    But if I make the same calculation without the exponential formula within a a loop:
    > my $end_val = $start_val; 10000 > $end_val *= 1.016 for 1..10 Nil > say $end_val 11720.25550356773576542599 > say $end_val.WHAT (Rat) > say $end_val.nude (17464541649174296506384 1490116119384765625)
    As you can see, I still get a Rat. And, BTW, I get a better accuracy with this loop, but, in fact, we don't really care about this better accuracy, since the final monetary amount will be rounded to two decimal places anyway (although you could conceivably find start values for the error on the floating point value would propagate and make a 1 cent difference on the final rounded value). To tell the truth, I was fairly lucky here: with just 11 years, I would no longer get a Rat.

    And, of course, the loop will fall back to floats if we compound the interests monthly:

    > my $end_val = $start_val * (1 + 0.016/12)**120 11733.8581431786 > say $end_val.WHAT (Num) > > my $end_val = $start_val; 10000 > $end_val *= 1 + 0.016/12 for 1..120 Nil > say $end_val 11733.8581431787 > say $end_val.WHAT (Num)
    We get the same result (there is a rounding difference of 1 on the last digit, but we don't care, it will be rounded to 11,733.86 anyway).

    So yes, we often fall back on floats when compounding interests, but that really doesn't matter, nobody cares.

    A company for which I worked as a freelance consultant had to spend tens of thousands of euros in a software project to fix invoices which looked wrong because of rounding problems on FP arithmetic inaccuracies. One invoice in tens of thousands looked wrong (the total was actually accurate, but the individual values and subtotals did not seem to match); some consumers complained and the legal and/or tax authorities demanded a correction. All these amounts would have matched properly if the calculations had been done with Perl 6 Rat type.

      I get a better accuracy with this loop, but, in fact, we don't really care about this better accuracy, since the final monetary amount will be rounded to two decimal places anyway (although you could conceivably find start values for the error on the floating point value would propagate and make a 1 cent difference on the final rounded value). To tell the truth, I was fairly lucky here: with just 11 years, I would no longer get a Rat.

      Please tell me you don't work on financial applications. This would be unacceptable behavior for any application in my purview.

      (I've recently worked on financial applications. "It doesn't matter" and "it doesn't happen that often" is doubly unacceptable if you care about auditing your finances or if you process more than a few thousand values.)

        Please tell me you don't work on financial applications. This would be unacceptable behavior for any application in my purview.
        Then, with all due respect, I am afraid that you probably did not read what I wrote in this post and other posts in this thread with enough care.

        And please don't take my sentences out of context. When multiplying a single value by a single certain factor, whether the 15th decimal place of that factor is accurate or not is usually completely irrelevant. And anyone having worked long enough with finance, accounting and audit departments know that these people care much more about "looking good" than about being accurate. If they cared about accuracy, they wouldn't ask you to fill your monthly time sheets tens days before the end of the month.

      As you can see, I still get a Rat.
      Now try looping eleven times. It becomes a Num.
      All these amounts would have matched properly if the calculations had been done with Perl 6 Rat type.
      Maybe, maybe not. Did you try implementing it in Perl6 to see if it worked?

      You're saying that Rationals are more accurate, and at the same time "that really doesn't matter, nobody cares." You have a level of cognitive dissonance going on that I just can't seem to break through.

        Haha, I missed the fact that you knew 11 times would be a Num. Wow, don't you realize that your post refutes itself? Alright, peace out.
Re^9: Reasons for Using Perl 6
by Laurent_R (Canon) on Jan 03, 2018 at 20:19 UTC
    > 3*10**-20 - 2*10**-20 - 1*10**-20 -1.50463276905253e-36 # oops
    Oh, yeah, that's really a very common use case.
      Perl6: optimized for the trivial case(TM).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1206623]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2018-07-21 13:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (449 votes). Check out past polls.

    Notices?