Python weirding out?

Started by
1 comment, last by GarrickW 12 years, 11 months ago
I must be making some kind of embarassingly simple mistake, here, but I just can't see it.

I've got a very basic equation in my program that looks like this:

self.xProportion = 1.0 * self.xDistance / self.totalDistance

self.xDistance is a negative integer, such as -87 (literally, that is the value it had when I last run the program). self.totalDistance is a positive integer, in this case, 183. I stuck the 1.0 in there because I want a float; this technique has worked for me in the past.

Yet no matter what the values for the two variables are, I get an end result of 0. I can't think of that many other ways to do this. The following permutation gives me a result of -1.

self.xProportion = float(self.xDistance / self.totalDistance)

The following gives me a result of 0.

self.xProportion = float(self.xDistance) / float(self.totalDistance)

Alone in the console, however, when I ask Python what 1.0 * -87 / 183 gives me, I get -0.47540983606557374, which is exactly what I want.

So what's going on here? Am I missing something?

For reference, the larger part of the program in which the code appears:


def moveOrder(self, point):
#Used setting up movement directly towards a particular node..
self.xDistance = self.mapPosX - point[0]
self.yDistance = self.mapPosY - point[1]
print "xDistance: %i" % (self.xDistance)
print "yDistance: %i" % (self.yDistance)
#Establish a total distance.
self.totalDistance = abs(self.xDistance) + abs(self.yDistance)

#If the distance is too small, don't bother moving.
if self.totalDistance <= 2:
self.moving = False
return

#The amount of total moveSpeed dedicated to each direction
#is proportional to the weight that direction has in the distance.

#Establish which proportion of that distance is X, which Y
self.xProportion = 1.0 * self.xDistance / self.totalDistance
self.yProportion = 1.0 * self.yDistance / self.totalDistance
print "xProportion: %i" % (self.xProportion)
print "yProportion: %i" % (self.yProportion)
print "Equation: %i = 1.0 * %i / %i" % (self.xProportion, self.xDistance, self.yDistance)
#Proportionalize movement speed.
self.xSpeed = self.moveSpeed * self.xProportion
self.ySpeed = self.moveSpeed * self.yProportion
print "xSpeed: %i" % (self.xSpeed)
print "ySpeed: %i" % (self.ySpeed)

self.moving = True
Advertisement
The problem is in your print statement. You're telling print to format the value as an integer (%i). Try %f instead.
Gods below, that's embarrassing. Thank you, sir!

This topic is closed to new replies.

Advertisement