float / double support in Python?

Alex Martelli aleax at aleax.it
Fri Feb 7 13:26:02 CET 2003

jsaul wrote:
> I.e. int/float division gives the same result as int/int if the
> savespace flag is set. Fine. However, I would expect »a*0.5« to
> return the same as »a/2.«, which is not the case.

The savespace (which might be better named the "sticky type")
flag coerces the OTHER argument.  So, a*b when a has savespace
set and a typecode of 'i' becomes equivalent to a*int(b), and
similarly a/b becomes equivalent to a/int(b).

So why would you expect a*int(0.5), i.e. a*0, to give the
same results as a/int(2), i.e. a/2?

If you want an array to have "sticky type", then set its
savespace flag and it will -- as you had originally asked.

If you DON'T want that, leave the array alone (or reset
its savespace flag explicitly) and you will get default
behavior, i.e., coercion widens the narrower type.

Maybe what you want is something different again and more
elaborate, such as, perform all computations AS IF savespace
was unset, and then coerce everything back to some narrower
type.  If such is indeed what you want, you'll have to
program it explicitly (it's not hard, of course).


More information about the Python-list mailing list