01.0 == 10 ???

Discussion in 'Perl Misc' started by kj, Jul 16, 2005.

  1. kj

    kj Guest

    Check this out:

    % perl -wle 'print 01'
    1
    % perl -wle 'print 1.'
    1
    % perl -wle 'print 01.'
    syntax error at -e line 1, at EOF
    -e had compilation errors.
    % perl -wle 'print 01.0'
    10
    % perl -wle 'print +( 01.0 == 10 ? "weird!" : "whew!" )'
    weird!

    What's up with that? 01.0 is equal to 10???

    I am also hoping that the explanation for this will also explain
    why "01." triggers a syntax error, but not "01.0". If not, I'm
    curious about that too.

    If it weren't for the fact that "01.0" produces no errors, I would
    have surmised that the syntax error triggered by "01." had something
    to do with it being interpreted as an attempt to specify an "octal
    float", but the fact that perl takes "01.0" without any objetion
    (albeit doing something pretty strange with it) makes me question
    this hypothesis.

    Thanks!

    kj

    --
    NOTE: In my address everything before the first period is backwards;
    and the last period, and everything after it, should be discarded.
    kj, Jul 16, 2005
    #1
    1. Advertising

  2. kj wrote:
    > Check this out:
    >
    > % perl -wle 'print 01'
    > 1
    > % perl -wle 'print 1.'
    > 1
    > % perl -wle 'print 01.'
    > syntax error at -e line 1, at EOF
    > -e had compilation errors.
    > % perl -wle 'print 01.0'
    > 10
    > % perl -wle 'print +( 01.0 == 10 ? "weird!" : "whew!" )'
    > weird!
    >
    > What's up with that? 01.0 is equal to 10???
    > I am also hoping that the explanation for this will also explain
    > why "01." triggers a syntax error, but not "01.0". If not, I'm
    > curious about that too.


    Yes a number with a leading zero is interpreted in octal. But perl does
    _not_ recognise the period as an octal point so for example a
    one-and-a-half cannot be written 01.4.

    So 01 is the number one in octal.

    The period in the string concatenation operator.

    0 is the number zero.

    Concateation converts numeric operands into strings using decimal so one
    and zero become "1" and "0" hence 01.0 is the string "10".

    The == operator converts "10" to the number ten which is equal to the
    other number ten.
    Brian McCauley, Jul 16, 2005
    #2
    1. Advertising

  3. kj

    Joe Smith Guest

    kj wrote:

    > % perl -wle 'print 01.'
    > syntax error at -e line 1, at EOF
    > -e had compilation errors.
    > % perl -wle 'print 01.0'
    > 10


    Here's some more:

    % perl -le 'print 020.abc'
    16abc
    % perl -le 'print 009'
    Illegal octal digit '9' at -e line 1, at end of line
    Execution of -e aborted due to compilation errors.

    In the first one, 020 (octal) is 16 (decimal), and barewords
    act like quoted literals, same as 'print 16."abc"'.

    The second one is self explanatory once you recognize that
    a leading zero in numeric literals in the source code indicates
    an octal number. (And "0xFF" is hexadecimal for 255.)
    -Joe
    Joe Smith, Jul 17, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page