Monday 5 November 2018

Python float division "rounding error" on division by 100

When dividing a float by 100 in Python 2.7 I get the following "rounding behaviour":



>>> 3.7e-03/100

3.7000000000000005e-05


I would expect the following:



>>> 3.7e-03/100
3.7e-05


Note that:




>>> 3.7e-03/100 == 3.7e-05
False
>>> 3.7000000000000005e-05 == 3.7e-05
False


While probably of not practical difference in most applications I find this behaviour somewhat disconcerting.



Why does this happen and how can I avoid it?




I am using Python:
'2.7.5 |Anaconda 1.7.0 (32-bit)| (default, Jul 1 2013, 12:41:55) [MSC v.1500 32 bit (Intel)]'

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...