Update: Since posting, I've realised that I probably haven't accurately addressed the actual situation as there appears to be other weird stuff going on ... which will have to keep until another day.
I had, for example, overlooked the oddness of Num(0.7777777777777777777770) > Num(0.7777777777777777777771) being true. As regards 53-bit (double) precision those 2 values should be equivalent.
I get the same on rakudo-star-2017.07:
C:\>perl6 -e "say Num(0.7777777777777777777770) > Num(0.77777777777777
+77777771);"
True
But then:
C:\>perl6 -e "say Num(0.7777777777777777777770e0) > Num(0.777777777777
+7777777771e0);"
False
C:\>perl6 -e "say Num(0.7777777777777777777770e0) == Num(0.77777777777
+77777777771e0);"
True
So I probably need to better understand the rules that apply to Num().
Huh. Now they print the same, but they’re still different numbers when compared
When you say()/print() the number it gets rounded to 15 decimal digits - and it's quite common that different doubles (and therefore different values) will round to the same 15 decimal digit precision value.
It's exactly the same situation as with perl5 (which also prints out values rounded to 15 decimal digits).
If say()/print() rounded the doubles to 17 decimal digits of precision this type of discrepancy would be avoided.
Also interesting is that many Nums don’t survive a round-trip to Str
I think it's the same issue - to "survive" that "round-trip" there are many values that would require Str() to return 17 decimal digits of precision.
This is the reason that many languages (eg Go, Haskell, Python3, Ruby, Swift) do output 17 decimal digits.
It's also the reason that the mpfr C library outputs 17 decimal digits (by default) for 53-bit precision floating point values.
Can anyone point me to the Perl6 specs/docs/whatever that explain this behavior?
The explanation is found in the math.
In order to guarantee surviving the round-trip experience, the number of decimal digits required is given by the expression 1+ceil(P*log(2)/log(10)), where P is the precision (in bits) of the given floating point value.
This equates to 17, 21 and 36 decimal digits for (respectively) 53, 64 and 113 bit floating point values.
I don't know of any documentation (for either perl5 or perl6) that explains the choice of 15 decimal digits.
I haven't even seen any compelling justifications of that choice ... though I have seen some pretty lame excuses for it.
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.