Python Forum
Python 3- binary math is stupid - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Python 3- binary math is stupid (/thread-11242.html)



Python 3- binary math is stupid - FlyingGaijin - Jun-29-2018

One of these things doesn't do math like the other. (Python 3.5.5)

>>> import numpy as np
>>> np.arange(35.5,29.5,-0.1)
array([35.5, 35.4, 35.3, 35.2, 35.1, 35. , 34.9, 34.8, 34.7, 34.6, 34.5,
       34.4, 34.3, 34.2, 34.1, 34. , 33.9, 33.8, 33.7, 33.6, 33.5, 33.4,
       33.3, 33.2, 33.1, 33. , 32.9, 32.8, 32.7, 32.6, 32.5, 32.4, 32.3,
       32.2, 32.1, 32. , 31.9, 31.8, 31.7, 31.6, 31.5, 31.4, 31.3, 31.2,
       31.1, 31. , 30.9, 30.8, 30.7, 30.6, 30.5, 30.4, 30.3, 30.2, 30.1,
       30. , 29.9, 29.8, 29.7, 29.6)
>>> np.arange(35.5,29.4,-0.1)
array([35.5, 35.4, 35.3, 35.2, 35.1, 35. , 34.9, 34.8, 34.7, 34.6, 34.5,
       34.4, 34.3, 34.2, 34.1, 34. , 33.9, 33.8, 33.7, 33.6, 33.5, 33.4,
       33.3, 33.2, 33.1, 33. , 32.9, 32.8, 32.7, 32.6, 32.5, 32.4, 32.3,
       32.2, 32.1, 32. , 31.9, 31.8, 31.7, 31.6, 31.5, 31.4, 31.3, 31.2,
       31.1, 31. , 30.9, 30.8, 30.7, 30.6, 30.5, 30.4, 30.3, 30.2, 30.1,
       30. , 29.9, 29.8, 29.7, 29.6, 29.5, 29.4])
>>> np.arange(355.0,294.0,-1)/10
array([35.5, 35.4, 35.3, 35.2, 35.1, 35. , 34.9, 34.8, 34.7, 34.6, 34.5,
       34.4, 34.3, 34.2, 34.1, 34. , 33.9, 33.8, 33.7, 33.6, 33.5, 33.4,
       33.3, 33.2, 33.1, 33. , 32.9, 32.8, 32.7, 32.6, 32.5, 32.4, 32.3,
       32.2, 32.1, 32. , 31.9, 31.8, 31.7, 31.6, 31.5, 31.4, 31.3, 31.2,
       31.1, 31. , 30.9, 30.8, 30.7, 30.6, 30.5, 30.4, 30.3, 30.2, 30.1,
       30. , 29.9, 29.8, 29.7, 29.6, 29.5])



RE: Python 3- binary math is stupid - ichabod801 - Jun-29-2018

I would expect this is a floating point error, where 29.5 - 0.1 is not sufficiently different from 29.4. That doesn't mean it's different math, it just means that different numbers with different floating point approximations are going to trip this off. As always, if you need to be certain use integers and divide, like your third example.