remainder() in C++

C++Server Side ProgrammingProgramming

Here we will see the functionality of remainder() method of C++. The remainder() function is used to compute the floating point remainder of numerator/denominator.

So the remainder(x, y) will be like below.

remainder(x, y) = x – rquote * y

The rquote is the value of x/y. This is rounded towards the nearest integral value. This function takes two arguments of type double, float, long double, and returns the remainder of the same type, that was given as argument. The first argument is numerator, and the second argument is the denominator.

Example

#include <iostream>
#include <cmath>
using namespace std;
main() {
   double x = 14.5, y = 4.1;
   double res = remainder(x, y);
   cout << "Remainder of " << x << "/" << y << " is: " << res << endl;
   x = -34.50;
   y = 4.0;
   res = remainder(x, y);
   cout << "Remainder of " << x << "/" << y << " is: " << res << endl;
   x = 65.23;
   y = 0;
   res = remainder(x, y);
   cout << "Remainder of " << x << "/" << y << " is: " << res << endl;
}

Output

Remainder of 14.5/4.1 is: -1.9
Remainder of -34.5/4 is: 1.5
Remainder of 65.23/0 is: nan
raja
Published on 02-May-2019 15:35:10
Advertisements