C++ Standard Library C++ STL Library

C++ <cmath> - remquo() Function



The C++ <cmath> remquo() function returns the floating-point remainder of x/y (rounded to nearest integer). The function additionally stores the quotient internally which used to determine the result. This can be mathematically expressed as below:

remainder = x - quotient * y

Where quotient is the result of x/y rounded towards nearest integer (with halfway cases rounded toward the even number).

Note: The remainder function is similar to the remquo function except it does not stores quotient internally.

Syntax

double remquo (double x, double y, int* q);
float remquo (float x, float y, int* q);
long double remquo (long double x, long double y, int* q);
double remquo (Type1 x, Type2 y, int* q);                 

Parameters

x Specify the value of numerator.
y Specify the value of denominator.
q Specify the pointer to the quotient calculated internally and used to determine the remainder.

Return Value

Returns remainder of x/y. If the remainder is zero, its sign will be same as that of x and value stored in quotient will be unspecified. 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, remquo() function is used to find out the remainder and quotient of a given division.

#include <iostream>
#include <cmath>
using namespace std;
 
int main (){
  double x, y;
  int q;
  x = 23;
  y = 4;

  double result = remquo(x, y, &q);
  
  cout<<"numerator = "<<x<<"\n";
  cout<<"denominator = "<<y<<"\n";
  cout<<"remainder = "<<result<<"\n";
  cout<<"quotient = "<<q<<"\n";
 
  return 0;
}

The output of the above code will be:

numerator = 23
denominator = 4
remainder = -1
quotient = 6

❮ C++ <cmath> Library