[Python-Dev] Floor division
Tim Peters
tim.peters at gmail.com
Fri Jan 26 02:15:13 CET 2007
[Guido]
> ...
> I don't care about the speed, but having to import math (which I
> otherwise almost never need) is a distraction, and (perhaps more so) I
> can never remember whether it's modf() or fmod() that I want.
fractional part of x == fmod(x, 1.0) == modf(x)[0], so you could use
either. Since modf returns a tuple and fmod returns a float, you'll
get an exception quickly if you pick the wrong one :-) The name
"modf" certainly sucks.
...
>>> assuming int() continues to truncate.
>> Has anyone suggested to change that? I'm not aware of any complaints
>> or problems due to int() truncating. There have been requests to add
>> new kinds of round-to-integer functions, but in addition to int().
> I thought those semantics were kind of poorly specified. But maybe
> that was long ago (when int() just did whatever (int) did in C) and
> it's part of the language now.
"(int)float_or_double" truncates in C (even in K&R C) /provided that/
the true result is representable as an int. Else behavior is
undefined (may return -1, may cause a HW fault, ...).
So Python uses C's modf() for float->int now, which is always defined
for finite floats, and also truncates.
More information about the Python-Dev
mailing list