Maybe add `from __future__ import division` just to be safe; you're doing a lot of calculations here.