473,387 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1218
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Allcomp | last post by:
Hello, I have seen something really strange in VB6 If I do a Int ( (5 * 1.2)) , I receive the value 5, but I should receive 6? Is this a bug or something really "normal". I can see that if I...
0
by: han zhiyang | last post by:
I've just studied the "how to" web service and the async pattern in donnet.I make a test with these knowledges,but I got a strange result. Here is my test. 1.Write a simple "Add" service named...
2
by: Webdiyer | last post by:
Hi, We all know that the return value of Math.Log(8,2) is 3,but how about (int)Math.Log(8,2)? On my machine,the return value of (int)Math.Log(8,2) is strange enough! it's not 3 but 2 ! I've...
5
by: Vedran Furač | last post by:
I think that this results must be the same: In : math.atan2(-0.0,-1) Out: -3.1415926535897931 In : math.atan2(-0,-1) Out: 3.1415926535897931 In : -0 == -0.0 Out: True
10
by: Michele | last post by:
Please forgive me for the neverending code down here but I cannot find a rational explanation of the output of this simple program (really!). Soluzione class has a double field to represent a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.