snegovick / bcam

CAM system for hobbyists and shapeokers
GNU General Public License v3.0
22 stars 4 forks source link

Use explicit division #130

Closed randyheydon closed 9 years ago

randyheydon commented 9 years ago

By default in Python 2, division behaves differently based on the types of the two operands. 1/2 (integer divided by integer) results in floor division, so the result is 0. However, 1.0/2 uses true division, giving a result of 0.5.

As of Python 2.2 (through PEP 238), it has been possible to explcitly choose which division type to use. Floor division can be forced with the // operator (so 1.0//2.0 == 0.0), and all other division can be switched to true division by adding from __future__ import division to the top of the file (so 1/2 == 0.5). In Python 3, true division is the default behaviour.

I am trying to change BCAM to use division explicitly, but to do that I need to know what form of division is intended each time its used. I grepped for / to find all instances of division. Some of them are obvious as they include things like x/2.0, but others are not. Running python2 -Q warnall bcam-launcher will print warnings every time division is used, showing which type is used where. However, I found that several lines in main.py would do both, sometimes doing floor division and sometimes doing true division. Furthermore, there were a number of division operations that I wasn't able to trigger, as I don't know enough about the code to reach them.

In conclusion, please replace all intended floor divisions with the // operator, then add the division future import to each file. This will make the code more explicit in its intent, and is another step toward Python 3 compatibility.

snegovick commented 9 years ago

Thank you, after fixing divisions, Ive checked with python2 -Q warnall bcam-launcher , it only shows GTK-related issues now, also hand-tested bcam, it works, but definetely bcam needs better way for auto testing.