Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: why are hex values not numbers?

by perl-diddler (Chaplain)
on Oct 15, 2016 at 00:54 UTC ( [id://1174044]=note: print w/replies, xml ) Need Help??


in reply to Re: why are hex values not numbers?
in thread why are hex values not numbers?

So perl says that 0xAA looks like a number.
Fine.

Then why doesn't "0xAA"?

Replies are listed 'Best First'.
Re^3: why are hex values not numbers?
by BrowserUk (Patriarch) on Oct 15, 2016 at 01:22 UTC
    Because it's in quotes. Thus a string. And so could be the start of a string that contains (the base 64 string): "0x1a2b3c4d5e6f7q"
Re^3: why are hex values not numbers?
by haukex (Archbishop) on Oct 26, 2016 at 11:43 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1174044]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found