C++ remquo()

The remquo() function in C++ computes the floating point remainder of numerator/denominator and also stores the quotient to the pointer passed to it.

The remquo() function in C++ computes the floating point remainder of numerator/denominator (rounded to nearest). It also stores the quotient to the pointer passed to it. It returns the same value as remainder() function.


remquo() prototype [As of C++ 11 standard]

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); // Additional overloads for other combinations of arithmetic types.

The remquo() function takes a three arguments and returns a value of type double, float or long double type. This function is defined in <cmath> header file.


remquo() Parameters

  • x: The value of numerator.
  • y: The value of denominator.
  • q: Pointer to an object where the quotient internally used to determine the remainder is stored as a value of type int.

remquo() Return value

The remquo() function returns the floating point remainder of x/y (rounded to nearest). If the denominator y is zero, remquo() returns NaN (Not a Number).


Example 1: How remquo() works in C++?

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int q;
    double x = 12.5, y = 2.2;

    double result = remquo(x, y, &q);
    cout << "Remainder of " << x << "/" << y << " = " << result << endl;
    cout << "Quotient of " << x << "/" << y << " = " << q << endl << endl;

    x = -12.5;
    result = remquo(x, y, &q);
    cout << "Remainder of " << x << "/" << y << " = " << result << endl;
    cout << "Quotient of " << x << "/" << y << " = " << q << endl << endl;

    y = 0;
    result = remquo(x, y, &q);
    cout << "Remainder of " << x << "/" << y << " = " << result << endl;
    cout << "Quotient of " << x << "/" << y << " = " << q << endl << endl;
    
    return 0;
}

When you run the program, the output will be:

Remainder of 12.5/2.2 = -0.7
Quotient of 12.5/2.2 = 6

Remainder of -12.5/2.2 = 0.7
Quotient of -12.5/2.2 = -6

Remainder of -12.5/0 = -nan
Quotient of -12.5/0 = 0

Example 2: remquo() function for arguments of different types

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int q;
    double x = 12.5
    int y = 10;
    
    result = remquo(x, y, &q);
    cout << "Remainder of " << x << "/" << y << " = " << result << endl;
    
    return 0;
}

When you run the program, the output will be:

Remainder of 12.5/10 = 2.5
Quotient of 12.5/10 = 1
Did you find this article helpful?