Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: why are hex values not numbers?

by haukex (Archbishop)
on Sep 27, 2016 at 08:18 UTC ( [id://1172726]=note: print w/replies, xml ) Need Help??


in reply to why are hex values not numbers?

Hi perl-diddler,

By the way, Perl's internal API function that decides whether a string looks like a number is exposed by Scalar::Util's looks_like_number:

use Scalar::Util qw/looks_like_number/; print looks_like_number("65" )?"yes":"no", "\n"; # yes print looks_like_number("0x41" )?"yes":"no", "\n"; # no print looks_like_number( 0xA_A )?"yes":"no", "\n"; # yes print looks_like_number("0xA_A")?"yes":"no", "\n"; # no

In the second-to-last example, the literal 0xA_A becomes 170, which of course looks like a number.

Regards,
-- Hauke D

Replies are listed 'Best First'.
Re^2: why are hex values not numbers?
by perl-diddler (Chaplain) on Oct 15, 2016 at 00:54 UTC
    So perl says that 0xAA looks like a number.
    Fine.

    Then why doesn't "0xAA"?

      Because it's in quotes. Thus a string. And so could be the start of a string that contains (the base 64 string): "0x1a2b3c4d5e6f7q"

      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://1172726]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found