CKTIS / C
remainder — floating-point remainder
remainder computes a remainder using the nearest integer quotient, resolving halfway cases by choosing the even integer.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <math.h>
double remainder(double x, double y);remainder from <math.h> accepts and returns double. For finite arguments with y != 0, it computes x - n*y: n is the integer nearest the exact x / y, with ties choosing even, regardless of rounding mode.
Unlike fmod, the result can have the opposite sign to x. A zero result retains the sign of x. The % operator requires integer operands.
For y == 0, the base C specification allows a domain error to occur. If no domain error occurs, the specification does not guarantee a zero result. Under Annex F, a zero divisor or infinite x produces NaN and raises invalid when neither argument is NaN.
Examples
#include <math.h>
#include <stdio.h>
int main(void)
{
printf("%.0f\n", remainder(7.0, 2.0));
printf("%.0f\n", remainder(5.0, 2.0));
return 0;
}
Result
-1 1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.