In this C++ tutorial, you will learn how to find floating point remainder of x/y rounded to the nearest, using remainder() function of cmath, with syntax and examples.

C++ remainder()

C++ remainder(x, y) returns floating point remainder of x/y rounded to the nearest.

Syntax

The syntax of C++ remainder() is

</>
Copy
remainder(x, y)

where

ParameterDescription
xA double, float, long double, or integral type value.
yA double, float, long double, or integral type value.

Returns

The return value depends on the type of value passed for parameters x and y. The return value of remainder() is

  • double if x and y are double or integral type values.
  • float if x and y are float.
  • long double if x and y are long double.

The synopsis of remainder() function is

</>
Copy
double remainder(double x, double y);
float remainder(float x, float y);
long double remainder(long double x, long double y);
double remainder(Type1 x, Type2 y); // for integral type argument values

remainder() is a function of cmath library. Include cmath library at the start of program, if using remainder() function.

Example

In this example, we read two values from user into x and y, and find the remainder of x/y using remainder() function.

C++ Program

</>
Copy
#include <iostream>
#include<cmath>
using namespace std;

int main() {
    double x, y;
    cout << "Enter x : ";
    cin >> x;
    cout << "Enter y : ";
    cin >> y;

    double result = remainder(x, y);
    cout << "remainder(x, y) : " << result << endl;
}

Output

Enter x : 5
Enter y : 2.33
remainder(x, y) : 0.34
Program ended with exit code: 0
Enter x : 5.633
Enter y : 1.33
remainder(x, y) : 0.313
Program ended with exit code: 0
Enter x : 2
Enter y : inf
remainder(x, y) : 2
Program ended with exit code: 0
Enter x : inf
Enter y : 2
remainder(x, y) : nan
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned the syntax of C++ remainder(), and how to use this function to find the floating point remainder of x/y, with the help of examples.