Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^2: Research into occasional participation in Perl/Raku development

by talexb (Chancellor)
on Oct 01, 2021 at 23:57 UTC ( #11137187=note: print w/replies, xml ) Need Help??

in reply to Re: Research into occasional participation in Perl/Raku development
in thread Research into occasional participation in Perl/Raku development

Moving on to fractions, when I heard that raku was going to deal with fractions in a clever way, I decided to experiment and write my own module to do fractions. Instead of adulting tonight, I decided to spend a little time fiddling with code.

Here's my test script:

#!/usr/bin/perl use strict; use warnings; # OK, try the division of the large numbers using my # Number::MyFraction module. No idea what result I'll get. use lib '../Number-MyFractions/lib'; use Number::MyFraction; { my $long_tenth = Number::MyFraction->new( 3602879701896397, 36028797018963968 ); my $short_tenth = Number::MyFraction->new( 1, 10 ); my $difference = $long_tenth - $short_tenth; print "Compare long tenth: " . ( $long_tenth->decimal_val == .1 ? 'TRUE' : 'FALSE' ) . "\n"; print "Compare short tenth: " . ( $short_tenth->decimal_val == .1 ? 'TRUE' : 'FALSE' ) . "\n"; print "The different is " . $difference->decimal_val . ".\n"; }
Interestingly, it produced OK results ..
$ perl Compare long tenth: TRUE Compare short tenth: TRUE The different is 5.55111512312578e-18. $
Sure, the difference between the values is non-zero, but comparing the individual fractions to one tenth did work.

I think it just depends on which tool you use to get the job done.

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Replies are listed 'Best First'.
Re^3: Research into occasional participation in Perl/Raku development
by syphilis (Archbishop) on Oct 02, 2021 at 02:48 UTC
    First up, I apologise for tainting this thread with that previous post of mine.
    I need to keep in mind that raku was not written for me, but written for a group of people who find that raku suits them, and who are quite happy with what raku provides - and that I have no business complaining about anything that raku does.

    I think it just depends on which tool you use to get the job done.

    I see just 2 ways to compare rationals and doubles:
    1) convert the rational to a double and compare the two doubles;
    2) convert the double to a rational and compare the two rationals.

    The former is probably the easier, and that's the approach that raku takes.
    But, in converting a rational to a double, it is often the case that precision is lost, and the double thus obtained is merely an approximation of the rational value from which it was derived.
    OTOH, every finite double can be represented exactly as a rational - and it therefore makes sense to me that they should be compared via the latter option (like python3 does), as we are then always comparing exact values.
    There's a caveat here in that the numerators/denominators needed to express some doubles can be rather large, and this could be viewed as a dis-incentive to using this second option.

    Perl doesn't have a rational scalar, and C doesn't have a rational data type, so neither of those languages have needed to think about how rationals and floats should be compared.
    (Of course, perl has a core Math::BigRat module in which the overloading of comparision operators needed to be considered.)


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2022-05-20 04:59 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (72 votes). Check out past polls.