HELP: restore my faith in Python

Moshe Zadka moshez at math.huji.ac.il
Sat Mar 4 10:25:17 CET 2000


On Fri, 3 Mar 2000, Holton, Steven [NCRTP:6125:EXCH] wrote:

> 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

Is there a special reson you're re-writing the '%' operator?

> ...    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?)

If you don't know what you're doing, don't deal with floats. I'm sure
Tim Peters, as a former HW floating-point guy can remember exactly how
many digits you need to do it correctly, but the short answer is "more
then your CPU has".

floating-points-are-*not*-a-field-by-a-long-shot-ly y'rs, Z.
--
Moshe Zadka <mzadka at geocities.com>. 
http://www.oreilly.com/news/prescod_0300.html





More information about the Python-list mailing list