How do I do floating point Remainder function in C++?

M

Mr. Ken

Morning, group:

How do I code floating point modulus function?
What I need is

double a;
double b;
double c;

a = 2.3;
b = 1.2;

c = a % b; // invalid operands of types `double' and `int' to binary `

TIA.
 
G

Gernot Frisch

a - b*int(a/b) or fmod() should do the trick.

when b == 0, you're busted. When a/b > MAX_INT, the result is wrong.

Use fmod, which basically does:
f = a/b - floor(a/b);
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top