[Python-bugs-list] Inaccuracy of <= to two or more decimal places (PR#351)

Tim Peters tim_one@email.msn.com
Sun, 11 Jun 2000 14:24:45 -0400


As Skip said, this is a property of binary floating-point arithmetic
regardless of language.  Your basic problem is that 0.05 is not exactly
representable in binary floating-point, and that's a bit hidden from you
because Python doesn't print floating-point values to full precision by
default.  Try replacing

      print LoopVariable

with

      print "%.17g" % LoopVariable

to see more of what's really happening.  On my box, that prints

0.050000000000000003
0.10000000000000001
0.15000000000000002
0.20000000000000001
0.25
0.29999999999999999
0.34999999999999998
0.39999999999999997
0.44999999999999996
0.49999999999999994
0.54999999999999993
0.59999999999999998
0.65000000000000002
0.70000000000000007
0.75000000000000011
0.80000000000000016
0.8500000000000002
0.90000000000000024
0.95000000000000029

> -----Original Message-----
> From: python-bugs-list-admin@python.org
> [mailto:python-bugs-list-admin@python.org]On Behalf Of ashar@vpharm.com
> Sent: Thursday, June 08, 2000 6:02 PM
> To: python-bugs-list@python.org
> Cc: bugs-py@python.org
> Subject: [Python-bugs-list] Inaccuracy of <= to two or more decimal
> places (PR#351)
>
>
> Full_Name: Raj Ashar
> Version: 1.5.2
> OS: Windows NT 4.0
> Submission from: fringe.vpharm.com (208.48.225.2)
>
>
> Hello. K. Sandretto and I believe we found the "<=" operator functions
> like a "<" when incrementing loop variables with numbers smaller than 0.1.
>
> For example:
> LoopVariable = 0
> Increment = 0.05
>
> while LoopVariable <= 1:
>       print LoopVariable
>       LoopVariable = LoopVariable + Increment
>
> prints out 0.95 as the highest value, as the "<" operator would,
> rather than printing out 1.0 as well.
>
> When only the Increment was changed to 0.1 in this loop,
> the value 1.0 did get printed onscreen. Using the value 0.125
> also printed out 1.0, but neither 0.025 nor 0.01 allowed 1.0 to appear
> onscreen.
>
> We have been able to also reproduce this result in Python 1.5 on an SGI
> running IRIX 6.4, and we appreciate any ideas on this matter.
>
>
> Raj
>
>
>
>
> _______________________________________________
> Python-bugs-list maillist  -  Python-bugs-list@python.org
> http://www.python.org/mailman/listinfo/python-bugs-list
>