Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the function remquo do and what can it be used for?

Tags:

c

function

Reading through the C specs I found this function:

double remquo(double x, double y, int *quo);
float remquof(float x, float y, int *quo);
long double remquol(long double x, long double y,
    int *quo);

The remquo functions compute the same remainder as the remainder functions. In the object pointed to by quo they store a value whose sign is the sign of x/y and whose magnitude is congruent modulo 2^n to the magnitude of the integral quotient of x/y, where n is an implementation-defined integer greater than or equal to 3.

The remquo functions return x REM y. If y is zero, the value stored in the object pointed to by quo is unspecified and whether a domain error occurs or the functions return zero is implementation defined.

I understand what it returns, it returns fmod(x, y), but I don't understand the whole quo part. Is it semantically equal to this?

*quo = (int) x/y;
*quo %= n; /* n implementation defined */

And my last question, for what could this function be useful?

like image 976
orlp Avatar asked Feb 19 '12 14:02

orlp


1 Answers

A typical usage of remquo() is the reduction of trigonometry function arguments. The last 3 bits of the quotient would allow one to tell which semi-quadrant an angle resides in, after a reduction modulo Pi/4, and to convert the original trig call into another trig call over an angle within the [0,Pi/4) interval (the new trig function could be different). The latter is usually computed via a Pade approximation.

like image 61
cruncher Avatar answered Sep 28 '22 18:09

cruncher