I m trying to find out a simple modulus operation on float data type.
float a=3.14f;
float b=10f;
result=a%b;
I m getting result= 3.14
Another example using decimal data types:
decimal p=10;
decimal q=40;
result=p%q;
getting answer=20.
I am not understanding how does modulus works?
% operator cannot be used with floating point numbers in C & C++.
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. So the remainder(x, y) will be like below.
As we learn in first paragraph, in Java modulus, operator can also be applied to floating-point numbers e.g. 3.0%1.0 is perfectly legal in Java. Since it return remainder value in division operation it is also known as remainder operator.
Datatypes of Operands to Modulus Operator C++ Modulus Operator takes only signed or unsigned integer datatypes.
From the C# language spec on floating point remainder. In the case of x % y
if x
and y
are positive finite values.
z
is the result ofx % y
and is computed asx – n * y
, wheren
is the largest possible integer that is less than or equal tox / y
.
The C# language spec also clearly outlines the table of what to do with the cases of all possible combinations of nonzero finite values, zeros, infinities, and NaN’s which can occur with floating point values of x % y.
y value | +y –y +0 –0 +∞ –∞ NaN -----+---------------------------- x +x | +z +z NaN NaN x x NaN –x | –z –z NaN NaN –x –x NaN v +0 | +0 +0 NaN NaN +0 +0 NaN a –0 | –0 –0 NaN NaN –0 –0 NaN l +∞ | NaN NaN NaN NaN NaN NaN NaN u –∞ | NaN NaN NaN NaN NaN NaN NaN e NaN | NaN NaN NaN NaN NaN NaN NaN
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