C++ cmath remquo() Function
Example
Divide 11 by 3, and get both the quotient and the remainder:
int q;
double r = remquo(11.0, 3.0, &q);
cout << q << "\n";
cout << r;
Definition and Usage
The remquo() function divides x by y, and rounds the result (the quotient) to the nearest integer. It writes the quotient to the int that the pointer z points to, and returns the remainder. In the example, &q gives the memory address of q, so the function can change q.
11.0 / 3.0 is about 3.67, which rounds to 4. The remainder is 11 - 4 * 3, which is -1. This is the same result as remainder() gives.
The remquo function is defined in the <cmath> header file.
Note: The quotient is only sure to be correct when it is from -7 to 7. For bigger quotients, only the sign and the last 3 bits are correct, and the value can be different between compilers.
Syntax
remquo(double x, double y, int* z)
The function also works with float and long double values. Then it returns a value of the same type.
Parameter Values
| Parameter | Description |
|---|---|
| x | Required. The number to divide (the dividend). |
| y | Required. The number to divide by (the divisor). |
| z | Required. A pointer to an int. The rounded quotient is written to this int. |
Technical Details
| Returns: | A double value with the remainder. |
|---|
Related Pages
Read more about math in our Math Tutorial.