strange math?

J

joe.hrbek

Hello everyone, I'm experimenting with python and i'm following this
tutorial:
http://docs.python.org/tut/node6.html#SECTION006400000000000000000 I'm
in section 4.7.5 Lambda Forms. In this section I was working along and
I noticed something strange. It happened because of a typo. Below is
a copy/paste from my idle session:
return lambda x: x+n
50

The first f(01) was a mistake. I accidentally forgot to delete the
zero, but to my suprise, it yielded the result I expected. So, I tried
it again, and viola, the right answer. So, I decided to really try and
throw it for a loop, f(010), and it produced 50. I expected 52
(42+10). Why doesn't python ignore the first zero and produce a result
of 52? It ignored the first zero for f(01) and f(02). Hmm. I know, I
know, why am I sending it a 01,02, or a 010 to begin with? Like I
said, it was an accident, but now i'm curious. I'm not a computer
science major so please be kind with any explanations.
 
S

Steven Bethard

50

The first f(01) was a mistake. I accidentally forgot to delete the
zero, but to my suprise, it yielded the result I expected. So, I tried
it again, and viola, the right answer. So, I decided to really try and
throw it for a loop, f(010), and it produced 50. I expected 52
(42+10). Why doesn't python ignore the first zero and produce a result
of 52? It ignored the first zero for f(01) and f(02). Hmm. I know, I
know, why am I sending it a 01,02, or a 010 to begin with? Like I
said, it was an accident, but now i'm curious. I'm not a computer
science major so please be kind with any explanations.

Python isn't ignoring the initial 0. It reads the initial 0 as
indicating that the following number is expressed in octal. You can see
this if you try any number with a digit higher than 7 in it:
Traceback ( File "<interactive input>", line 1
08
^
SyntaxError: invalid token

So you get 8 added to your number when you write 010 because eight is
spelled as 10 in octal.

The leading-zero notation is unfortunate, and there has been some recent
discussion[1][2] on python-dev about trying to change the prefix to
``0o`` or ``0c`` in Python 3.0 but for the moment at least it looks like
we're stuck with the confusing leading zero.

[1]http://mail.python.org/pipermail/python-dev/2006-January/060262.html
[2]http://mail.python.org/pipermail/python-dev/2006-February/060277.html

STeVe
 
C

Christian Stapfer

Hello everyone, I'm experimenting with python and i'm following this
tutorial:
http://docs.python.org/tut/node6.html#SECTION006400000000000000000 I'm
in section 4.7.5 Lambda Forms. In this section I was working along and
I noticed something strange. It happened because of a typo. Below is
a copy/paste from my idle session:

return lambda x: x+n

50

The first f(01) was a mistake. I accidentally forgot to delete the
zero, but to my suprise, it yielded the result I expected. So, I tried
it again, and viola, the right answer. So, I decided to really try and
throw it for a loop, f(010), and it produced 50. I expected 52
(42+10). Why doesn't python ignore the first zero and produce a result
of 52?

That's because python interprets 010 as *octal* 10
which is *decimal* 8. Thus

42+010 = 42+8 = 50

which is quite as it should be...

Regards,
Christian
 
T

Terry Reedy


Literal ints with lead 0 are interpreted as octal numbers (base 8) instead
of decimal numbers (base 10). 01==1 either way, but 010 == 8.8

0x (zero eks) prefix indicate hexadecimal numbers:255

Terry Jan Reedy
 
R

Ron Adam

50

The first f(01) was a mistake. I accidentally forgot to delete the
zero, but to my suprise, it yielded the result I expected. So, I tried
it again, and viola, the right answer. So, I decided to really try and
throw it for a loop, f(010), and it produced 50. I expected 52
(42+10). Why doesn't python ignore the first zero and produce a result
of 52? It ignored the first zero for f(01) and f(02). Hmm. I know, I
know, why am I sending it a 01,02, or a 010 to begin with? Like I
said, it was an accident, but now i'm curious. I'm not a computer
science major so please be kind with any explanations.

Number beginning with the digit '0' are octal (base 8), so 010 == 8.
42 + 8 = 50.

Numbers beginning with '0x' are base 16.

Cheers,
Ron
 

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

No members online now.

Forum statistics

Threads
474,291
Messages
2,571,455
Members
48,132
Latest member
KatlynC08

Latest Threads

Top