"Peter Pippinger" <pe*************@gmx.de> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
Hello NG,
i need to calculate the modulus with double precisson typed variables.
this works well with integers:
w1 = w1 % 360;
but what do i have to do if i want to use w1 as double?
Well, you have the problem that floating point division does not have a
remainder, otherwise it wouldn't be floating point. But, there is still a
solution.
Take for an example
14.5 / 3.5;
The answer is:
4.1428571428571428571428571428571
So what would the remainder be defined as? This would be how many times 3.5
goes into 14.5 wholy. We see it goes 4 times plus a little bit. This is
easy to figure out. Take the answer as an integer. Multiply it by the
original divider. Then subtract this by the original divider.
float a = 14.5;
float b = 3.5;
int result = static_cast<int>( a / b );
float mod = a - static_cast<float>( result ) * b;