01.0 == 10 ???

K

kj

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
 
B

Brian McCauley

kj said:
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.
 
J

Joe Smith

kj said:
% 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top