Fun with numbers - dammit, but I want a cast!
ark at acm.org
Mon Aug 11 15:51:20 CEST 2003
Graham> I'm having some fun with numbers. I've extraced an image
Graham> sizes from a jpeg file
Graham> then I'm trying to use those sizes to scale the image, but
Graham> because python has decided that they are integers, I keep
Graham> getting division by zero errors
Graham> where xframe will be say 300, and img_x will be 1800
Graham> xscale has the value 0.
Graham> I've tried doing img_x=1.0 to force it to be a float, but I'm
Graham> getting a bit frustrated as it seems to make no difference -
Graham> once image.getsize returns the (integer) value of the x size,
Graham> it simply converts back to an int.
1) Create a floating-point value from the dividend or divisor,
which will cause the other one to be converted to float:
xscale = float(xframe) / img_x
xscale = xframe / float(img_x)
2) At the beginning of your source file, execute:
from __future__ import division
Python is going to change its behavior so that division always
returns a floating-point value. This statement causes that new
behavior to occur now.
Andrew Koenig, ark at acm.org
More information about the Python-list