We don't bite newbies here... much | |
PerlMonks |
Re^10: Perl 5 numeric type and simplificationsby syphilis (Archbishop) |
on May 08, 2021 at 15:24 UTC ( [id://11132277]=note: print w/replies, xml ) | Need Help?? |
... 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: 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
In Section
Seekers of Perl Wisdom
|
|