0

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?

3
  • 1
    I doubt you executed the last code manually in 2.7. Integer division in 2.7 will yield an integer, not float, and 9/99 will result in 0. Have a look at python.org/dev/peps/pep-0238 Commented Feb 3, 2012 at 12:41
  • @FelixKling See my edit. I originally forgot from __future__ import division Commented Feb 3, 2012 at 12:45
  • @dr.bunsen: Please note that, yes, you will get the error with the code you show. But the answers point out that you need to have the from __future__ import division in the file that defines A.rescale(). I do not believe you have that. Commented Feb 3, 2012 at 17:43

3 Answers 3

8

You need to set from __future__ import division in the file that contains the divisions, i.e. in the file with A.

Sign up to request clarification or add additional context in comments.

5 Comments

I have from __future__ import division in the file and I do not believe that this is the problem.
Well, I've said exactly that.
@wRAR - this is most certainly the correct answer. Failing to have his import in the file containing the class defintion for A would yield the problem you are reporting
For some reason the OP deleted his comment stating that my suggestion fiexed his problem.
I've tested the code as given, and it works under 2.7 as well as Python 3. Removing the __future__ makes it fail as described in the question. I believe you are correct. This is the problem.
3

For python 2.7 you can either:

 x.rescale(1.0, 10.0, 1.0, 100.0, 5.0) 

Or you could explicitly cast to float in division inside method.

 scale = float((old_max - old_min)) / (new_max - new_min) 

Or another way is to import from __future__ import division.

This is because in python 2.x integer divided by integer will result in an integer, in your case 0.

EDIT after your comment:

Make sure you do the

 from __future__ import division 

IN module A, as there is where the computations are done not like you did there.

Comments

1

Using this code in 2.7:

from __future__ import division 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 x = A() print x.rescale(1,10,1,100,5) 

gives me:

45.0 

If I remove the __future__ import I get this:

Traceback (most recent call last): File "test.py", line 15, in <module> print x.rescale(1,10,1,100,5) File "test.py", line 11, in rescale rescaled_value = new_min + ((value - old_min) / (scale)) ZeroDivisionError: integer division or modulo by zero 

You must have the from __future__ import division line in the module that defines A. Imports only affect the module they are contained in.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.