I have to use '%' with double numbers, but in C++ it doesn't work. Example:
double x;
temp = x%10;
I get this error:
error C2296: '%' : illegal, left operand has type 'double'
How can I solve this problem without converting the number from double to integer? If I converted it, I will lose the fractional part, and I don't want.
Is there another alternative?
%
is not defined for doubles, but you can use fmod
instead:
Compute remainder of division Returns the floating-point remainder of numer/denom (rounded towards zero):
Example (adapted for C++) from http://www.cplusplus.com/reference/cmath/fmod/:
#include <cmath> /* fmod */
#include <iostream>
int main ()
{
std::cout << "fmod of 5.3 / 2 is " << std::fmod (5.3, 2) << std::endl;
return 0;
}
Use the fmod
function
#include <math.h>
double x;
temp = fmod(x, 10.0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With