getting the fractional part of a real?

R

Roy Smith

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)?
 
M

Mark Dickinson

(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
 
R

Roy Smith

Gregor Lingl said:
Or without the need to import something:


Gregor

Thanks. I knew I had to be missing something obvious.

Regarding Mark Dickinson's question about what I want for negative reals,
for the application I have in mind, r is guaranteed to be positive.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top