... 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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.