HELP: restore my faith in Python
Holton, Steven [NCRTP:6125:EXCH]
sholton at americasm10.nt.com
Fri Mar 3 14:06:46 EST 2000
I'm trying to learn Python, and wrote this script last night. I was not
expecting to see this behavior, and my faith is severely shaken:
nose-46> python
Python 1.5.2 (#1, Sep 17 1999, 20:15:36) [GCC egcs-2.91.66
19990314/Linux (egcs- on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> limit = 5
>>> for target in range(10):
... n=((target/(limit+0.0))-(target/limit))*limit
... print target, n, int(n)
...
0 0.0 0
1 1.0 1
2 2.0 2
3 3.0 3
4 4.0 4
5 0.0 0
6 1.0 0 <=== huh?
7 2.0 1 <=== et tu Brutus?
8 3.0 3
9 4.0 4
Column 1 is the index, column 2 is 'n' as floating, and column 3 is
'n' as an integer. Except that it ain't.
Can anyone explaing to me why index 6 and 7 aren't what I'm expecting
them to be?
6 / 5.0 is 1.2 (when we're dealing with floats)
6 / 5 is 1 (when we're dealing with ints)
1.2 - 1 = 0.2
0.2 * 5 = 1.0
int( 1.0 ) = 1 (don't it?)
>>> 6 / 5.0
1.2
>>> 6 / 5
1
>>> 1.2 - 1
0.2
>>> 0.2 * 5
1.0
>>> int( 1.0 )
1
--
sholton at nortelnetworks.com
More information about the Python-list
mailing list