C++ <cmath> - fmod() Function
The C++ <cmath> fmod() function returns the floating-point remainder of x/y (rounded towards zero). This can be mathematically expressed as below:
fmod = x - tquot * y
Where tquot is the result of x/y rounded toward zero.
Note: The remainder function is similar to the fmod function except the quotient is rounded to the nearest integer instead of towards zero.
Syntax
double fmod (double x, double y); float fmod (float x, float y); long double fmod (long double x, long double y);
double fmod (double x, double y); float fmod (float x, float y); long double fmod (long double x, long double y); //additional overloads double fmod (Type1 x, Type2 y);
Parameters
x |
Specify the value of numerator. |
y |
Specify the value of denominator. |
Return Value
Returns remainder of x/y. If y is zero, the function may either return zero or cause a domain error (depending on the library implementation).
Example:
In the example below, fmod() function is used to find out the remainder of a given division.
#include <iostream> #include <cmath> using namespace std; int main (){ cout<<fmod(25, 4)<<"\n"; cout<<fmod(20, 4.9)<<"\n"; cout<<fmod(20.5, 2.1)<<"\n"; return 0; }
The output of the above code will be:
1 0.4 1.6
❮ C++ <cmath> Library