Hi perl-diddler,

I think BrowserUk made a good point, I just wanted to expand on that: numeric literals really are different from strings. Numeric literals are numbers; the literal 0xAA means the number 170. In this example, note especially the last item in the list:

$ perl -wMstrict -le '$,=", "; print 42, 0xFF, 0b1111, 42 + 0xFF + 0b1 +111, 42 . 0xFF . 0b1111' 42, 255, 15, 312, 4225515

Perl converts the literals internally before doing anything else with them.

Consider that there are limits on the size of integers that you can normally store in memory:

$ perl -wMstrict -le 'print 0xFFFFFFFF' 4294967295 $ perl -wMstrict -le 'print 0xFFFFFFFFF' Hexadecimal number > 0xffffffff non-portable at -e line 1. 68719476735 $ perl -wMstrict -le 'print 0xFFFFFFFFFFFFFFFFF' Integer overflow in hexadecimal number at -e line 1. Hexadecimal number > 0xffffffff non-portable at -e line 1. 2.95147905179353e+20

Note that in each of the above examples, the results are exactly the same if you put a hex("...") around the literals.

Next, consider that there isn't a similar limit on the size of strings:

$ perl -wMstrict -le 'print hex( "0x".("F" x 2**29) )' Integer overflow in hexadecimal number at -e line 1. Hexadecimal number > 0xffffffff non-portable at -e line 1. Inf

That string, which is over 500MB, is much more likely to be some kind of data than a number - does it make sense that it should be converted to Inf or some other floating-point number if the user accidentally uses it in numeric context? (Note even Math::BigInt can't handle interpreting a hex number that big on my system!)

Now, in theory, Perl could convert hexadecimal strings that are "small enough" to numbers automagically. But in such a theoretical solution, would it make sense that 0+"0xFFFFFFFF" eq "4294967295", but 0+"0x1FFFFFFFF" eq "0"? Where does one draw the line which numbers are "small enough", and won't such a division cause confusion for the users? (Let's ignore for now the potential performance impact of adding the code to check for hexadecimal to Perl's grok_number_flags.)

Instead, the design choice that literal numbers are fundamentally different from strings makes things much more consistent. Sure, one will have to call hex and oct a few times, but on the other hand, the argument can be made that calling those functions explicitly gives the code more clarity.

Hope this helps,
-- Hauke D


In reply to Re^3: why are hex values not numbers? by haukex
in thread why are hex values not numbers? by perl-diddler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.