Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^10: Perl 5 numeric type and simplifications

by syphilis (Archbishop)
on May 08, 2021 at 15:24 UTC ( [id://11132277]=note: print w/replies, xml ) Need Help??


in reply to Re^9: Perl 5 numeric type and simplifications
in thread Perl 5 numeric type and simplifications

... but this is not a CS forum

++LanX
We've finally arrived at consensus ;-)

And I've no issue with your update to your original post:

Some simple rational numbers (e.g., 1/3 and 1/10) cannot be represented exactly in binary floating point, no matter what the precision is

Indeed ... and one of those two numbers you've quoted cannot even be represented exactly in decimal floating point, no matter what the precision is.

Software packages that perform rational arithmetic represent numbers as fractions with integral numerator and denominator, and can therefore represent any rational number exactly

Yes, and we already have rational arithmetic in perl in the form of Math::BigRat (which I don't recommend) and Math::GMPq (which I do recommend).
I've also recently had a bit of a look at raku's rational implementation and I think that it's pretty good. They don't provide the speed of Math::GMPq, but then they don't require that the gmp library be installed - whilst still providing an implementation that works reasonably quickly

The link that you provided (to https://en.wikipedia.org/wiki/Floating-point_arithmetic#Alternatives_to_floating-point_numbers) is an interesting read.
As I go through the various bullet points, I find that most of them are accommodated in perl.
I don't think perl covers "Logarithmic number systems" and "Tapered floating-point representation". (I'm not familiar with either of those ... something TODO.)
But I'm inclined to think that Math::GMPn (by salva), Math::GMPz, Math::GMPq, Math::MPFR and Math::MPFI (for the interval arithmetic) have the rest covered.

Interestingly, as regards the final bullet point in that section, the mpfr library has only recently gained the capability of evaluating (the given example of) sin(3*pi) exactly - so, with Math::MPFR, we can now do:
C:\>perl -MMath::MPFR=":mpfr" -E "$rop=Math::MPFR->new(); $op=Math::MP +FR->new(3); $inex=Rmpfr_sinpi($rop, $op, MPFR_RNDN); say $inex; say $ +rop;" 0 0
Which gives us an exact evaluation of sin(3 * pi). (That $inex is 0 simply confirms that the result is exact. As you well know, such a result is impossible to achieve with rational arithmetic.)
At present, if you want to try out this particular feature, you'll need the current mpfr devel library and the current "devel" (ie github) Math-MPFR-4.17 module.

Cheers,
Rob

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2024-04-25 00:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found