Possible Duplicate:
Why does modulus division (%
) only work with integers?
This code doesn't work in C and C++ but works in C# and Java:
float x = 3.4f % 1.1f;
double x = 3.4 % 1.1;
Also, division remainder is defined for reals in Python.
What is the reason this operation is not defined for floats and doubles in C and C++?
% operator cannot be used with floating point numbers in C & C++.
15) Can you use C Modulo Division operator % with float and int? Explanation: Modulo Division operator % in C language can be used only with integer variables or constants.
The modulo operator, like the other arithmetic operators, can be used with the numeric types int and float .
The modulus is basically finding the remainder. For this, we can use the remainder() function in C. The remainder() function is used to compute the floating point remainder of numerator/denominator.
The C committee explained its position of why there is no remainder operator for floating types in the Rationale document:
(6.5.5 Multiplicative operators) The C89 Committee rejected extending the % operator to work on floating types as such usage would duplicate the facility provided by fmod (see §7.12.10.1).
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