7.2.05

 

remainder()

the standard C math library has a function;

SYNOPSIS
#include <math.h>

double remainder(double x, double y);

the function returns the remainder on dividing x by y.
this result is chosen to be between -y/2 and y/2. it's usual to choose remainders either between 0 and y, or between -y/2 and y/2. however, in the second case it is also usual to specify which of the 2 is chosen, if x = (n + 1/2)y.

DESCRIPTION
The remainder() function returns the floating point
remainder r = x - ny when y is non-zero. The value n is the
integral value nearest the exact value x/y. When |n - x/y| =
1/2, the value n is chosen to be even.

the last sentence means that both -y/2 and y/2 are possible remainders, and both are used. this means that this function, for a given y, returns different values for x's which are a multiple of y apart, or in other words, returns different values for numbers which have the same remainder on division by y.
#include <stdio.h>
#include <math.h>

int main()
{
printf("%d\n", (int)remainder(13,26));
printf("%d\n", (int)remainder(39,26));
return 0;
}

9 % cc remainder.c -lm -o remainder
10 % remainder
13
-13
this is a pain.

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?