Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: why are hex values not numbers?

by perl-diddler (Chaplain)
on Sep 26, 2016 at 21:43 UTC ( [id://1172680]=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?

But I think you missed the point.

The "dec" isn't needed. That conversion happens without the explicit 'dec'.

That's why I compared it against the shell equivalent, that makes no such differentiation. Perl *does* accept both decimal and hex outside of quotes, so why is the eval needed for hex numbers from strings? Seems a bit odd for a computer language.

Replies are listed 'Best First'.
Re^3: why are hex values not numbers?
by hippo (Bishop) on Sep 26, 2016 at 21:49 UTC

    Because decimals alone are special inside string literals. perldata states:

    Hexadecimal, octal, or binary, representations in string literals (e.g. '0xff') are not automatically converted to their integer representation. The hex() and oct() functions make these conversions for you. See hex and oct for more details.

    Consider the decimal autoconversion as a bit of handy magic, nothing more.

      But it doesn't say "why". I don't see a reason ...?
Re^3: why are hex values not numbers?
by BrowserUk (Patriarch) on Sep 27, 2016 at 00:02 UTC
    why is the eval needed for hex numbers from strings? Seems a bit odd for a computer language.

    In many (most) other languages -- I'd post a list, but the PM post size limit is something like 64kb -- you would have to explicitly convert the string containing a decimal number also -- see atoi(), atoi64(), atof() etc. in your local C compiler's CTR manual.

    Your expectations are naive; and your protestations more so.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Maybe you could just mention 1 mainstream language (besides perl) that doesn't accept hex or octal constants where it accepts decimal. You list examples of needing functions to convert hex and octal -- but those other languages also need functions for decimal. In the actual *language*, that the compiler reads -- they accept octal and hex where they accept binary.

      So instead of 64k, how about 1?

      BTW -- please note BrowserUK's tag line below -- this part:

      "Science is about questioning the status quo. Questioning authority".

      Seemed especially pertinent to some peoples' reactions to my asking "why is this this way" (with some answering, "that's the way it is" (status quo), or "as documented" (implicit acceptance of authority).

      Um, BrowserUK -- do you walk your taglines, or are they just decorative? :-)

        that doesn't accept hex or octal constants

        You're conflating the issue. Mixing apples and blackberries in an attempt to make sanyos.

        Perl does accept hex or octal (and binary to boot) constants anywhere it accepts a decimal constant:

        print for 65, 0x41, 0101, 0b1000001;; 65 65 65 65

        In addition, it also accepts a string containing a decimal constant, anywhere it takes a number:

        print 65 + 0x41 + 0101 + 0x1000001 + "3.141592653589793238462643383279 +5e001";; 16777443.4159265

        Making it more flexible than most other languages.

        The fact that it does not also accept strings containing non-decimal numeric values is not a language flaw; but a design decision, which if you think about it a little, makes perfect sense.

        A string containing just decimal digits, can only be a number.

        A string containing "OX84AB97" could be a color code; and one containing "007 James Bond" ...


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-24 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found