remquo, remquof, remquol
Defined in header <math.h>


float remquof( float x, float y, int *quo ); 
(1)  (since C99) 
double remquo( double x, double y, int *quo ); 
(2)  (since C99) 
long double remquol( long double x, long double y, int *quo ); 
(3)  (since C99) 
Defined in header <tgmath.h>


#define remquo( x, y, quo ) 
(4)  (since C99) 
remquol
is called. Otherwise, if any nonpointer argument has integer type or has type double, remquo
is called. Otherwise, remquof
is called.Contents 
[edit] Parameters
x, y    floating point values 
quo    pointer to an integer value to store the sign and some bits of x/y 
[edit] Return value
If successful, returns the floatingpoint remainder of the division x/y as defined in remainder, and a value whose sign is the sign of x/y and whose magnitude is congruent modulo 2n
to the magnitude of the integral quotient of x/y, where n is an implementationdefined integer greater than or equal to 3.
If y
is zero, the value stored in *quo is unspecified.
If a domain error occurs, an implementationdefined value is returned (NaN where supported)
If a range error occurs due to underflow, the correct result is returned if subnormals are supported.
If y
is zero, but the domain error does not occur, zero is returned.
[edit] Error handling
Errors are reported as specified in math_errhandling
Domain error may occur if y
is zero.
If the implementation supports IEEE floatingpoint arithmetic (IEC 60559),
 The current rounding mode has no effect.
 FE_INEXACT is never raised
 If
x
is ±∞ andy
is not NaN, NaN is returned and FE_INVALID is raised  If
y
is ±0 andx
is not NaN, NaN is returned and FE_INVALID is raised  If either
x
ory
is NaN, NaN is returned
[edit] Notes
POSIX requires that a domain error occurs if x
is infinite or y
is zero.
This function is useful when implementing periodic functions with the period exactly representable as a floatingpoint value: when calculating sin(πx) for a very large x
, calling sin directly may result in a large error, but if the function argument is first reduced with remquo
, he loworder bits of the quotient may be used to determine the sign and the octant of the result within the period, while the remainder may be used to calculate the value with high precision.
On some platforms this operation is supported by hardware (and, for example, on Intel CPU, FPREM1
leaves exactly 3 bits of precision in the quotient)
[edit] Example
#include <stdio.h> #include <math.h> #include <fenv.h> #pragma STDC FENV_ACCESS ON double cos_pi_x_naive(double x) { double pi = acos(1); return cos(pi * x); } // the period is 2, values are (0;0.5) positive, (0.5;1.5) negative, (1.5,2) positive double cos_pi_x_smart(double x) { int quadrant; double rem = remquo(x, 1, &quadrant); quadrant = (unsigned)quadrant % 4; // keep 2 bits to determine quadrant double pi = acos(1); switch(quadrant) { case 0: return cos(pi * rem); case 1: return cos(pi * rem); case 2: return cos(pi * rem); case 3: return cos(pi * rem); }; } int main(void) { printf("cos(pi * 0.25) = %f\n", cos_pi_x_naive(0.25)); printf("cos(pi * 1.25) = %f\n", cos_pi_x_naive(1.25)); printf("cos(pi * 1000000000000.25) = %f\n", cos_pi_x_naive(1000000000000.25)); printf("cos(pi * 1000000000001.25) = %f\n", cos_pi_x_naive(1000000000001.25)); printf("cos(pi * 1000000000000.25) = %f\n", cos_pi_x_smart(1000000000000.25)); printf("cos(pi * 1000000000001.25) = %f\n", cos_pi_x_smart(1000000000001.25)); // error handling feclearexcept(FE_ALL_EXCEPT); int quo; printf("remquo(+Inf, 1) = %.1f\n", remquo(INFINITY, 1, &quo)); if(fetestexcept(FE_INVALID)) puts(" FE_INVALID raised"); }
Possible output:
cos(pi * 0.25) = 0.707107 cos(pi * 1.25) = 0.707107 cos(pi * 1000000000000.25) = 0.707123 cos(pi * 1000000000001.25) = 0.707117 cos(pi * 1000000000000.25) = 0.707107 cos(pi * 1000000000001.25) = 0.707107 remquo(+Inf, 1) = nan FE_INVALID raised
[edit] See also
(C99) 
computes quotient and remainder of integer division (function) 
(C99)(C99) 
computes remainder of the floatingpoint division operation (function) 
(C99)(C99)(C99) 
computes signed remainder of the floatingpoint division operation (function) 
C++ documentation for remquo
