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

Re^15: why are hex values not numbers? (octal)

by tye (Sage)
on Oct 02, 2016 at 05:16 UTC ( #1173109=note: print w/replies, xml ) Need Help??

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

This thread gave no valid reason.

Well, most of the thread didn't even try to give a reason. But I think Re^2: why are hex values not numbers? got it right.

I recall finding a bug in C code that misinterpreted IP addresses padded to look nice:

by interpreting octets with leading zeros as being in octal.

Perl decided to require you to be explicit if you want to interpret a leading zero as indicating octal when interpreting a string. oct() is how to indicate that you want the leading zero to impact the base when interpreting a string as a number.

I concur with the choice to have "010" == 10 and "010" != 010. And I can see how it could seem more consistent to have the interpretation of 0x and 0b fall along the same lines.

But stepping away from that consistency, I agree with you that it would be good to transition Perl so that it becomes possible for "0x10" == 0x10 (and that such eventually becomes the default). And I would like to see similar changes so we get to where "1_000" == 1_000.

Beyond just a foolish consistency, it could also be argued that when asking a user for a numeric value, you should not assume that the user is a programmer. So you should treat "012" as "12" because that is what a non-programmer would mean. And you could extend that argument to other number formats that are expected of programmers but not others, like "0x1A4". But I disagree with that conclusion. "012" is a valid way to write 12. "0x12" is not a valid way to write 0.

- tye        

Replies are listed 'Best First'.
Re^16: why are hex values not numbers? (octal)
by BrowserUk (Patriarch) on Oct 02, 2016 at 10:56 UTC

      I had this thought before :)

      now perl gives warning

      Argument "0x41" isn't numeric in printf

      Very clean, things we know are numbers and can unambiguously identify as numbers, we treat as numbers, all other stuff we warn about

      But if definition of numeric was expanded to include more ambiguous num formats, then perl would need a new warning

      treating "0x41" as numeric , might be wrong thing to do, silence with +"no warnings q/extranumeric/;"

      I'm confused why perl-diddler hasn't posted use extranumeric; yet :D

        But if definition of numeric was expanded to include more ambiguous num formats, then perl would need a new warning: treating "0x41" as numeric , might be wrong thing to do, silence with "no warnings q/extranumeric/;"

        That's a (the first) plausible approach to the proposition. It is certainly doable; but at what benefit and what cost?

        Ignoring for now the old saw of backward compatibility. (Though it certainly wouldn't be ignored by the only persons whom opinions matter in this regard, namely the denizens of p5p.)

        The "benefit" would be that the programmer expecting non-decimal numbers in string input could write:

        print "%d\n", $string;

        Rather than:

        printf "%d\n", hex( $string );

        And the cost would be implicitly calling hex every time a scalar currently holding a PV, is used in a numeric context.

        That doesn't seem such a bad thing. (At least if it had been that way since the beginning: Ba-w-d co-pa-bil-ty!)

        But, either this is going to warn "might be wrong thing" every time it finds an extranumeric and the programmers who don't want that will have to disable it; or it will be silent and the programmers that do want the warning, will have to explicitly enable it.

        Seems to me that for those few that do, something like this is just as easy:

        sub myHex { no warnings 'digit'; hex( $_[0] }; } printf "%d\n", myHex( $string );

        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.
Re^16: why are hex values not numbers?
by perl-diddler (Chaplain) on Oct 03, 2016 at 12:25 UTC
    rewritten to reduce confusion.
      what was "rewritten to reduce confusion"?
Re^16: why are hex (not octal) values not numbers?
by perl-diddler (Chaplain) on Oct 03, 2016 at 18:15 UTC
    Um... If you are going to give a reason, I already ceded octal earlier in this discussion, for compatibility reasons. I limited my suggestion to constants beginning with "0x".

    As for 0xabcdef being a color code, that confusion already exists now, since color codes are, often, not in quotes. So the problem of "confusion" already exists.

    So I already excluded octal and someone came up with color codes in hex, which are already oft unquoted, so no increase in confusion w/that example.

    So back to the simple case of showing problems where allowing an auto-convert of 0x12 to 18, in a numeric context (where today, it would generate an error).

    Part of the problem in finding an example where it would be a problem is you'd have to present buggy code as the example. Code that only runs with a warning and generates '0' as a binary value when used in a numeric context. That's why detractors can't find an example: it would be an example of non-working code.

    Now does anyone get the point? The only way someone can accidently use "0x123456" as a binary value they can't! They get a warning and the constant "0x123457" evaluates as binary zero. There can be no confusion in current code, because trying to decode a hex constant in a string returns a zero.

    As for future code. Maybe allowing hex constants anywhere decimal constants are, as in almost any other language, won't be so confusing to "just perl programmers", since programmers in other languages don't get so easily confused -- why would anyone protest that perl programmers would?

      I already ceded octal: So, now it's only hex-like strings you want to make a special case of. The ideas wearing a bit thin isn't. someone came up with color codes in hex, which are already oft unquoted,: It doesn't have to be quotes -- its in a string. It's only you wants to be able to treat that ambiguous string as a number.

      You're like a dog with a bone tied to its tail, chasing around in circles refusing to let go! Fatuous.

      A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2023-12-03 01:57 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (20 votes). Check out past polls.