I have a Python 3 class method for rescaling values that looks like this:
class A(object): """docstring for A""" def __init__(self): super(A, self).__init__() def rescale(self, old_min, old_max, new_min, new_max, value): """rescales a value given a current old_min and old_max to the desired new_min and new_max""" scale = (old_max - old_min) / (new_max - new_min) rescaled_value = new_min + ((value - old_min) / (scale)) return rescaled_value Using Python 3, this method works like this:
>>> import A >>> x = A() >>> x.rescale(1,10,1,100,5) 45.0 In Python 2.7, this code does not work:
>>> from __future__ import division >>> x = A() >>> x.rescale(1,10,1,100,5) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "bunsen.py", line 35, in rescale rescaled_value = new_min + ((value - old_min) / (scale)) ZeroDivisionError: integer division or modulo by zero If I manually do this math in Python 2.7 I get the correct answer:
>>> from __future__ import division >>> scale = (10 - 1) / (100 - 1) >>> rescaled_value = 1 + ((5 - 1) / (scale)) >>> rescaled_value 45.0 Can anyone point out why this method does not work in Python 2.7?
9/99will result in0. Have a look at python.org/dev/peps/pep-0238from __future__ import divisionfrom __future__ import divisionin the file that definesA.rescale(). I do not believe you have that.