Newbie: strange int() behaviour
Raymond Hettinger
vze4rx4y at verizon.net
Tue Dec 3 09:15:41 EST 2002
> My very first python script calculates a value for a given number, prints
> out a number of line conisting of stars (half the number) and the value
> itself.
>
> Out of some strange reason it looks like 100 / 2 is in one case 49. Why?
>
> The main loop:
> for i in range (80,100,1):
> val = fuz.process("temp",i,"speed")
Add this line:
print val # Will reveal that fuz.process returns
# slightly less than 100.0000 for i=87
> half = val /2
Here is the problem.
Val is a floating point number slightly less than 100.0
and the / 2 gives floor division, truncating to 49.
Try this line instead:
half = round(val / 2.0, 3)
> length = int(half)
> stars= length * "*"
> print "%s %s (%f) stars: %s" % (i,stars,val,length)
Raymond Hettinger
More information about the Python-list
mailing list