getting the fractional part of a real?
Mark Dickinson
dickinsm at gmail.com
Sat Aug 15 14:50:00 EDT 2009
On Aug 15, 7:40 pm, Christian Heimes <li... at cheimes.de> wrote:
> Roy Smith schrieb:
>
> > What's the best way to get the fractional part of a real? The two ways I
> > can see are r % 1 and r = int(r), but both seem a bit hokey. Is there
> > something more straight-forward that I'm missing, like fraction(r)?
> >>> import math
> >>> math.modf(1.5)
>
> (0.5, 1.0)
What Christian said. math.fmod(r, 1.0) also works.
Note that r % 1 and r - int(r) aren't the same thing for negative
reals.
What sign do you want the result to have when r is negative?
Mark
More information about the Python-list
mailing list