Ashley Bye
Ashley Bye

Reputation: 1750

C - floor of double moduls integer

I need to do the following equation floor(e%100000) where e is a double. I know mod only accepts int values, how do I go about achieving this same result?

Thanks

Upvotes: 1

Views: 362

Answers (4)

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59111

You could use division to make the equivalent of modulo:

double e = 1289401004400.589201;
const double divisor = 100000.0;
double remainder = e - floor(e / divisor) * divisor;
double result = floor(remainder);
printf("%f\n", result);

This prints

4400.000000

Of course, this is much slower than any built-in modulo...

You could also just use fmod, as Anders K. suggested :)

Edit

Fixed std::cout (C++) reference to use printf (C). Fixed change to output. Now it is purely C.

Upvotes: 0

slacker
slacker

Reputation: 2142

Use the fmod() function instead of %. It accepts double parameters, and returns a double result.

Upvotes: 3

AndersK
AndersK

Reputation: 36082

use fmod

Upvotes: 3

EMP
EMP

Reputation: 61981

Why don't you take the floor first, then mod, ie. floor(e) % 100000 ?

Perhaps I've misunderstood what you're trying to achieve. Could you give an example of the input and output you expect?

Upvotes: 3

Related Questions