In C++, std::remquo(x, y, &q) does two jobs in one call. It returns the IEEE remainder of x / y, the same value as std::remainder(). It also stores the whole-number quotient in the int that you give it.
The stored quotient only has to be correct in its sign and its last few bits (at least three). That is enough to find out which quarter turn an angle is in, which is why it is used in the code behind sin() and cos().
std::remquo() is declared in <cmath> and is part of the group Remainder, scale, and compare. These functions split a number, scale it by a power of two, find its remainder, or pick between two numbers. They work directly on the binary form of a double.
Syntax
Include <cmath> and call std::remquo. Start with these lines.
C++
int q;
double a = std::remquo(10.0, 3.0, &q); // a = 1, q = 3
double b = std::remquo(11.0, 3.0, &q); // b = -1, q = 4
Call
What it does
std::remquo(10.0, 3.0, &q)
Returns 1 and stores 3 in q.
std::remquo(11.0, 3.0, &q)
Returns -1 and stores 4 in q.
std::remquo(-10.0, 3.0, &q)
Returns -1 and stores -3. The sign of q follows x / y.
The third argument is a pointer to an int, so write &q. The quotient is rounded to the nearest whole number, the same as in remainder().
Library declarations
C++
float remquo(float x, float y, int* quo);
double remquo(double x, double y, int* quo);
long double remquo(long double x, long double y, int* quo);
Parameters
Parameter
Description
x
Required. The dividend. It can be float, double, or long double.
y
Required. The divisor. It must not be zero.
quo
Required. A pointer to an int. It receives the sign and at least the lowest three bits of the whole quotient.
Return Value
Return Value
Description
Remainder
The IEEE remainder x - n * y, between -|y|/2 and |y|/2.
NaN
Returned when y is 0, or when x is infinity or NaN. The value of *quo is then unspecified.
*quo
The quotient n (sign and low bits). The sign matches the sign of x / y.
C++ Examples
Example 1 — Remainder and Quotient Together
Call remquo() and print both the returned remainder and the stored quotient.
C++
#include <cmath>
#include <iostream>
int main() {
double pairs[][2] = {{10, 3}, {11, 3}, {-10, 3}};
for (auto& p : pairs) {
int q;
double r = std::remquo(p[0], p[1], &q);
std::cout << p[0] << " / " << p[1] << ": remainder " << r
<< ", quotient " << q << '\n';
}
return 0;
}
The divisor is a quarter turn, about 1.5708 radians. The remainder is the angle left over inside the closest quarter.
7.0 contains 4 quarters (4.46 rounded). q & 3 keeps only the lowest two bits, and 4 & 3 is 0, so the quadrant is 0 and the rest is 0.7168.
2.0 is 1.27 quarters, so q is 1 with 0.4292 left. 5.0 gives 3 quarters and 0.2876. The remainder can also be negative when the nearest quarter is above the angle.
Example 3 — How Ties Are Decided
When the quotient is exactly halfway, the nearest even whole number is used. See it with x / 2 for odd numbers.
C++
#include <cmath>
#include <iostream>
int main() {
for (double x : {5.0, 7.0, -5.0}) {
int q;
double r = std::remquo(x, 2.0, &q);
std::cout << x << " / 2: quotient " << q << ", remainder " << r
<< ", check " << (q * 2.0 + r) << '\n';
}
return 0;
}
5 / 2 is exactly 2.5, a tie. The even neighbour 2 is used, so q is 2 and the remainder is 5 - 4 = 1.
7 / 2 is 3.5. The even neighbour is 4, so q is 4 and the remainder is -1. For -5 / 2 the tie goes to -2, giving -1.
The last number checks the formula q * y + remainder. It gives back the original x every time, which proves that the pair of results is consistent.
Frequently Asked Questions
std::remquo(x, y, &q) returns the same remainder as std::remainder(x, y), and also stores the quotient (its sign and at least the last 3 bits) in the int q.
The IEEE remainder of x / y. The quotient is stored through the third argument, with the sign of x / y and at least the lowest 3 bits.
It is mostly used inside math code, such as reducing an angle before sin() or cos(). The quotient tells you which quarter turn the angle was in, and the remainder is the angle inside it.
For huge numbers the quotient may not fit in an int. The standard only promises the sign and at least the lowest 3 bits, which is all that is needed for tasks like finding a quadrant.
Include <cmath> and write std::remquo(). That always works and picks the C++ overloads. The global name ::remquo often works too, but the standard does not promise it, and it may only take a double.