fmod()
The fmod()
function returns the floating-point remainder of the division of two values (rounded towards zero). It is the extension of the modulo operation to floating-point numbers.
Syntax
fmod(numerator, denominator)
The quotient of the numerator
divided by the denominator
is rounded towards zero through truncation. The return type is a double
, float
, long double
, or a combination of these types.
If the denominator
is equal to zero, then either 0
, NaN
, or a domain error is returned. If a range error occurs, then the correct result is rounded and returned.
Note: The
<cmath>
header provides additional overloads for other combinations of arithmetic types (double
,float
, orlong double
). Overloaded functions cast the arguments to adouble
type before the calculation. If one of the arguments is along double
type, then both arguments are cast aslong double
types.
Example
The following example uses fmod()
to return the floating-point remainder of x
/y
as a double
type:
#include <iostream>#include <cmath>using namespace std;int main() {double x = 7.5, y = 2.1;double result = fmod(x, y);cout << "Remainder of " << x << "/" << y << " = " << result << endl;x = -17.50, y = 2.0;result = fmod(x, y);cout << "Remainder of " << x << "/" << y << " = " << result << endl;return 0;}
This will return the following output:
Remainder of 7.5/2.1 = 1.2Remainder of -17.5/2 = -1.5
Codebyte Example
The following example is runnable and returns the floating-point remainder:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours