So why does perl convert a number that will obviously fit inside a 64 bit integer to floating point?
Firstly, note that the
** operator always returns an NV:
C:\>perl -MDevel::Peek -le "$x = 2 ** 3; Dump $x;"
SV = NV(0x47cd88) at 0x47cda0
REFCNT = 1
FLAGS = (NOK,pNOK)
NV = 8
C:\>perl -MDevel::Peek -le "$x = 2 ** 49; Dump $x;"
SV = NV(0x33d148) at 0x33d160
REFCNT = 1
FLAGS = (NOK,pNOK)
NV = 562949953421312
C:\>perl -MDevel::Peek -le "$x = 2 ** 50; Dump $x;"
SV = NV(0x47cd38) at 0x47cd50
REFCNT = 1
FLAGS = (NOK,pNOK)
NV = 1.12589990684262e+15
(Maybe the question you should ask is "Why does the
** operator always return an NV ?". I don't know.)
What you've displayed is that perl sometimes decides that an NV will be printed (interpolated) as an integer, and sometimes printed as a float.
Obviously, non-integer NVs will always be printed as floats - no problems there.
Now, perl still adheres to the utterly fuckbrained notion that, when nvtype is double, print() will never present more than 15 significant digits.
And that's what you're seeing here.
The NV returned by the operation
2 ** 50 contains 16 significant digits which, by this idiotic rule, must be interpolated to 15 digits. And that's what perl duly does.
But if the NV contains an integer value comprising less than 16 significant digits that is in the range IV_MIN to UV_MAX (eg
2 ** 50 2 ** 49), then it will be interpolated as an integer.
On a perl whose nvtype is
long double fully quad IEEE 754 long double or __float128, you should find this particular anomaly to be absent - because the 15-digit limit does not apply there.
Thre's still an absurdly low limit, but it's large enough to prevent this particular case presented here.
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.