I have two float values, 'a' and 'b' .
I need to calculate the reminder of these two float values, and it must be a float value.
Let
float a = 1.1;
float b = 0.5;
So the remainder 'r' should be accurate value
i.e. r = a % b
r = 1.1 % 0.5
0.5) 1.1 (2
1.0
______
0.1
r = 0.1
But it causes to an error invalid operand for float values.
How to do it?
Remainder cannot be obtain in floating point division. Explanation: fmod(x,y) - Calculates x modulo y, the remainder of x/y. This function is the same as the modulus operator.
fmod() — Calculate Floating-Point RemainderThe fmod() function calculates the floating-point remainder of x/y. The absolute value of the result is always less than the absolute value of y. The result will have the same sign as x.
The remainder() function in C++ computes the floating point remainder of numerator/denominator (rounded to nearest). where rquote is the result of x/y , rounded towards the nearest integral value (with halfway cases rounded towards the even number).
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. The rquote is the value of x/y.
In C , C++ and Objective-C that would be fmod
.
use fmod()
#include <math.h>
double x,y,z;
x = 1.1;
y = 0.5;
z = fmod(x,y)
Don't tforget the -lm liker flag if you are on linux/unix/mac-osx/.
for more info
$man fmod
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