a print bug?

Duncan Booth duncan.booth at invalid.invalid
Fri Jul 28 04:48:15 EDT 2006


 wrote:

> 
> Duncan Booth wrote:
>> But you wouldn't complain about this would you?
>>
>> >>> print "%10.4f" % 1.23445
>>     1.2345
>> >>> print "%10.3f" % 1.23445
>>      1.234
>>
>> A value which is slightly than 1.2345 prints to 4 decimal places as
>> 1.2345 and to 3 decimal places as 1.234.
>>
>> That's all that happens with your value as well: 1.2345 is not
>> exactly representable as a floating point number, and the nearest
>> representable number is less than 1.2345.
> 
> This is the expected behavior though... even school when they first
> teach "rounding off", they will tell you 1.23445 rounding off to 3
> decimal places is not 1.235.... and i don't see anything weird about
> the two lines above.
> 
At least where I went to school they taught that the correct rounding was 
to the even digit, so 1.2335 and 1.2345 would both round to 1.234. 

However, that is irrelevant. The number in question here is 
1.2344999999999999 and however you were taught to round numbers that ought 
to round to 1.234 for 3 decimals, and 1.2345 for 4.




More information about the Python-list mailing list