in reply to Re^8: Reasons for Using Perl 6
in thread Reasons for Using Perl 6
But if I make the same calculation without the exponential formula within a a loop:> my $start_val = 10_000; 10000 > my $end_val = $start_val * (1 + 0.016)**10 11720.2555035677 > say $end_val.WHAT (Num)
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.> 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)
And, of course, the loop will fall back to floats if we compound the interests monthly:
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).> 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)
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^10: Reasons for Using Perl 6
by chromatic (Archbishop) on Jan 04, 2018 at 00:15 UTC | |
by Laurent_R (Canon) on Jan 04, 2018 at 08:29 UTC | |
by chromatic (Archbishop) on Jan 04, 2018 at 17:46 UTC | |
by Laurent_R (Canon) on Jan 04, 2018 at 19:00 UTC | |
by chromatic (Archbishop) on Jan 04, 2018 at 20:50 UTC | |
| |
by Anonymous Monk on Jan 04, 2018 at 19:09 UTC | |
by Anonymous Monk on Jan 04, 2018 at 15:06 UTC | |
by Laurent_R (Canon) on Jan 04, 2018 at 22:21 UTC | |
by Anonymous Monk on Jan 04, 2018 at 23:22 UTC | |
| |
|
Re^10: Reasons for Using Perl 6
by Anonymous Monk on Jan 03, 2018 at 23:17 UTC | |
by Anonymous Monk on Jan 03, 2018 at 23:19 UTC | |
by Laurent_R (Canon) on Jan 03, 2018 at 23:32 UTC | |
by Anonymous Monk on Jan 03, 2018 at 23:51 UTC | |
by Laurent_R (Canon) on Jan 04, 2018 at 00:14 UTC | |
|