Not at all. In fact, your snippet proves that the correct result is returned.
Of course the correct result is returned.
The "rubbish" part is for the print() function to display that result as
1.12589990684262e+15. The returned result is not 1.12589990684262e+15:
> perl -le "print '1.12589990684262e+15 is not equivalent to 2 ** 50'
+if 1.12589990684262e+15 != 2 ** 50;"
1.12589990684262e+15 is not equivalent to 2 ** 50
It's not an approximation.
Well, that depends upon what "it" is actually referring to.
Certainly, the returned value is not an approximation. But the value delivered by the print() function is most definitely an approximation.
In floating point notation, the exact correct presentation is 1.125899906842624e+15.
> perl -le "print '1.125899906842624e+15 is equivalent to 2 ** 50' if
+1.125899906842624e+15 == 2 ** 50;"
1.125899906842624e+15 is equivalent to 2 ** 50
I'm sure you're already aware of all of this.
I can see that I could have been clearer in my last post.
It's a PITA having to continually clarify whether one is talking about the the interpolated value (ie the value presented by the print function) or the actual returned value - and I might have got a bit lax about making the distinction.
By contrast, print() always provides accuracy in the values being displayed for IVs - so it's a pity that perl doesn't DWIM by performing integer exponentiation on positive integer operands (and return an IV) whenever integer overflow is not an issue.
Or,
alternatively, perl could start interpolating NVs in a sane way. (That's not an unreasonable expectation - python does it; raku does it.)
On perls where IVSIZE and NVSIZE are 8, I'm perpetually astounded that we allow (eg)
1 << 50 and
2 ** 50 to be interpolated to different values.
Cheers,
Rob
PS
In some rare moments of prolonged high-energy I did submit
a pre-RFC post to p5p about these issues with NV interpolation.
But I haven't managed to yet summons the energy to deal with the follow up actions requested in response to that post.
As I've mentioned there, my Math::MPFR module provides me with the sane behaviour I seek (coutesty of its
nvtoa() function), and it's far easier for me to use that whenever I want sane behaviour, rather than to get too fussed about coming up with an RFC that might then never be acted upon anyway.
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.