Re^6: Reasons for Using Perl 6
by Jenda (Abbot) on Jan 03, 2018 at 14:45 UTC

It will continue to "plague" us as soon as we venture outside mere division and multiplication. What's the rational value of sqrt(2)? What's the rational value of sin(25)?
Sure, you can use rational approximations instead of base 2 ones. For a fairly large expense.
Jenda
Enoch was right!
Enjoy the last years of Rome.
 [reply] [d/l] [select] 

Yes, Jenda, I know (and I said before) that the Rat type should be used for rational numbers, but would not help for irrational numbers. In whatever base, irrational numbers will be approximations. However, there are many many computer applications that would benefit from accuracy in simple additions or subtractions of rational numbers. For example most applications dealing with monetary amounts. 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.
 [reply] 

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.50463276905253e36 # oops
Monetary amounts? You will hit this error very fast if you start compounding interest.  [reply] [d/l] 








What's the rational value of sqrt(2)? What's the rational value of sin(25)?
If you want to deal with these things precisely, you can use Mathematica, or Python's Sympy.
>>> sqrt(2)
sqrt(2)
>>> sqrt(2) * sqrt(6)
2*sqrt(3)
>>> sin(pi*3/2)
1
Notice, that's exactly 1. No approximation at all. Sympy knows a lot of mathematical identities. For mathematical work, Python is so far ahead of Perl that it's downright embarrassing. To claim that Perl6 is breaking new ground in this area is simply absurd.  [reply] [d/l] 

For mathematical work, Python is so far ahead of Perl that it's downright embarrassing.
I agree that Python is ahead of Perl on that account and it's very unfortunate.
 [reply] 
Re^6: Reasons for Using Perl 6
by Anonymous Monk on Jan 03, 2018 at 16:20 UTC

Yes, I will probably convert 132511/43 into a FP approximate value only if I need it as a human to estimate the magnitude, but not if my aim is to store the value in a computer and if I am given the technical means to store it as a rational.
The trouble with rationals is that the denominators keep growing. If you add 132511/43 and 27/67 and 1024/853, you've got 7577076638/2457493. Pretty soon you'll hit Perl6's builtin limit of 64 bits in the denominator, and it will switch to binary floating point automatically. Rationals do not solve the problem, but they do make things a lot more complicated.
(Yes, I'm aware that you can sometimes cancel out common factors between the numerator and denominator. No, this does not solve your problem.)
 [reply] 

The trouble with rationals is that the denominators keep growing.
Yes and no. It is true that there are cases where they do grow (but do you often add numbers like 132511/43 and 27/67 and 1024/853?), but there are many more cases where they don't. If you add or subtract many numbers in decimal format which most of us use everyday (monetary amount, physical measures made in the metric system, etc.) with, say two to five decimal places (or more), and the denominators will not grow and will often be a power of 10 (possibly multiplied by a power of 2 or a power of 5, something that can easily be brought back to a power of 10 by adjusting the numerator by the same factor). So in such cases, you'll never reach the 64bit limit.
 [reply] 

If you add or subtract many numbers in decimal format which most of us use everyday...
If what you really want is BigDecimal, why bother with a rational type at all?
 [reply] 




