What is the fmod() function in C++?
Overview
The fmod() function is used to return the modulus value of a fraction of two floating-point numbers (x/y) passed to the function as an argument.
In simple terms, the fmod() function returns the floating-point remainder of .
Syntax
double fmod(double x, double y);
float fmod(float x, float y):
long double fmod(long double x, long double y);
Parameter value
The fmod() function takes two arguments (x and y) as its parameter values, where x and y represent the numerator and denominator, respectively.
Return value
The fmod() function returns the floating-point remainder or modulus of x and y. It returns a NaN value i.e (Not a Number) when the denominator y is given as zero.
Example
#include <iostream>#include <cmath>using namespace std;int main(){long double x = 10.55, y = 2.65;// using the fmod() functionlong double result = fmod(x, y);cout << "fmod(10.55, 2.65) = " << result << endl;x = -14.88, y = 2.06;// using the fmod() functionresult = fmod(x, y);cout << "fmod(-14.88, 2.06) =" << result << endl;return 0;}
Code explanation
-
Line 8: We declare two doubles,
xandy. -
Line 10: We use the
fmod()function to determine the remainder ofxdivided byy. -
Line 11: We print the output of the
fmod()function applied onxandy. -
Line 13: We assign new values to
xandy. -
Line 15: We use the
fmod()function to determine the remainder ofxdivided byy. -
Line 16: We print the output of the
fmod()function applied onxandy.