|
From: Scott K. <sc...@sc...> - 2001-01-02 22:29:25
|
Hello, my name is Scott and I am a Python newbie (2 months).
"hello Scott, what brought you here?"
;^)
Anyway, I am having the time of my programming life learning Python and
building a serious configuration program in Java/Jython. What I have
noticed when I am doing some loops is an annoying precision error that I
need to work around. The simple code:
#! /usr/bin/python
test_var = 0.0
limit = 0.1
inc = 0.01
while test_var <= limit:
print test_var
test_var = test_var + inc
always runs to 1 less loop than I need because of this output:
D:\dev\python\test>jython test_loop.py
0.0
0.01
0.02
0.03
0.04
0.05
0.060000000000000005
0.07
0.08
0.09
0.09999999999999999
With native Python 2.0 in the same platform and 1.52 on Linux, the
output is a little different:
D:\dev\python\test>test_loop.py
0.0
0.01
0.02
0.03
0.04
0.05
0.06
0.07
0.08
0.09
0.1
This is a little more of what I expected. This trivial example looks
harmless enough, but it gets out of hand when the loops are in the 100+
range. What happens is even the Python only runs to 0.99 instead of 1.0
like it should.
The only solution I have come up with is to slightly modify the
increment like:
test_var = round((test_var + inc), 2)
so that it cuts off the value every time through. This seems to work
well enough, but I am wondering if there is a better way.
I also wonder why Jython puts so many decimal places out when Python
keeps the original max of 2. This seems like it could be a Java thing
to me since I have seen similar behavior in Java programs.
Thanks in advance.
--
Scott Knight mailto:sc...@sc...
|