470,591 Members | 1,520 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

strange math?

Hello everyone, I'm experimenting with python and i'm following this
tutorial:
http://docs.python.org/tut/node6.htm...00000000000000 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:
def make_incrementor(n): return lambda x: x+n
f=make_incrementor(42)
f(0) 42f(1) 43f(10) 52f(0) 42f(01) 43f(02) 44f(010) 5042+010

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.

Mar 19 '06 #1
5 1113
jo*******@gmail.com wrote:
f(01) 43 f(02) 44 f(010) 50 42+010 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:
08

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
Mar 19 '06 #2
<jo*******@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
Hello everyone, I'm experimenting with python and i'm following this
tutorial:
http://docs.python.org/tut/node6.htm...00000000000000 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:
def make_incrementor(n): return lambda x: x+n
f=make_incrementor(42)
f(0) 42f(1) 43f(10) 52f(0) 42f(01) 43f(02) 44f(010) 5042+010

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
Mar 19 '06 #3
f(01) 43f(02) 44f(010) 5042+010 50


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.
010 8

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

255

Terry Jan Reedy

Mar 19 '06 #4
Thanks for the great reply, Steve, et al.

-j

Mar 19 '06 #5
jo*******@gmail.com wrote:
f(01) 43 f(02) 44 f(010) 50 42+010

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
Mar 19 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Allcomp | last post: by
5 posts views Thread by Vedran Furańć | last post: by
10 posts views Thread by Michele | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.