a print bug?

Steve Holden steve at holdenweb.com
Thu Jul 27 07:39:58 EDT 2006


Summercoolness at gmail.com wrote:
> it seems that the behavior of "print" is that it will round off
> properly for any floating point imperfection, such as shown in the
> first two samples.  The third sample seems to be a bug?  It doesn't
> know how to handle the floating imperfection in this case.
> 
> 
>>>>1.2345
> 
> 1.2344999999999999
> 
> 
>>>>print 1.2345
> 
> 1.2345
> 
> 
>>>>print "%10.3f" % 1.2345    # seems like a bug ----------------------------------
> 
>      1.234
> 
> 
>>>>print "%10.3f" % 1.23450001
> 
>      1.235
> 
> 
>>>>print "%10.3f" % 1.2344
> 
>      1.234
> 
> 
>>>>print "%10.3f" % 1.2346
> 
>      1.235
> 
  >>> print "%25.22f" % 1.2345
  1.2344999999999999000000

You obviously haven't yet passed your floating-point number proficiency 
test yet. Please restrict yourself to integers until you understand the 
difficulties that inaccuracies in floating-point can create ;-)

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list