remainder() Function
The remainder()
function computes the floating-point remainder of a division operation according to the IEC 60559 standard. It rounds the quotient to the nearest integer (with halfway cases rounded toward the even number) and returns the difference between the numerator and the product of the rounded quotient and the denominator. This function is useful for scenarios where the rounding of the quotient is critical.
Syntax of remainder()
double remainder(double numer, double denom);
Parameters
Parameter | Description |
---|---|
numer | Value of the numerator in the division. |
denom | Value of the denominator in the division. |
Return Value
The function returns the remainder of the division of given arguments. If this remainder is zero, its sign shall be that of numerator.
If denominator is zero, the function may either return zero or cause a domain error.
It is important to note that if the computed remainder is zero, its sign will match that of the numerator. Unlike fmod()
, which truncates the quotient, remainder()
uses rounding toward the nearest integral value, and a similar function remquo()
provides extra information about the quotient.
Examples for remainder()
Example 1: Basic Usage of remainder() for Positive Numbers
This example demonstrates how to compute the remainder for positive numerator and denominator values using remainder()
.
Program
#include <stdio.h>
#include <math.h>
int main() {
double a = 5.3;
double b = 2.0;
double result = remainder(a, b);
printf("Copied output: %f\n", result);
return 0;
}
Explanation:
- The program includes the necessary headers
<stdio.h>
and<math.h>
. - It computes the remainder of 5.3 divided by 2.0 using
remainder()
. - The computed result is printed to the console.
Program Output:
Copied output: -0.700000
Example 2: Handling Negative Numerator with remainder()
This example shows the effect of a negative numerator on the computed remainder. The sign of a zero remainder, if it occurs, will match that of the numerator.
Program
#include <stdio.h>
#include <math.h>
int main() {
double a = -5.3;
double b = 2.0;
double result = remainder(a, b);
printf("Copied output: %f\n", result);
return 0;
}
Explanation:
- The program computes the remainder for a negative numerator (-5.3) divided by a positive denominator (2.0).
- The function returns a remainder whose sign is influenced by the numerator.
- The result is printed to demonstrate how negative values are handled.
Program Output:
Copied output: 0.700000
Example 3: Comparing remainder() with fmod() Behavior
This example compares the behavior of remainder()
with fmod()
, highlighting the difference in how the quotient is rounded.
Program
#include <stdio.h>
#include <math.h>
int main() {
double a = 5.3;
double b = 2.0;
double rem_result = remainder(a, b);
double fmod_result = fmod(a, b);
printf("Copied remainder(): %f\n", rem_result);
printf("Copied fmod(): %f\n", fmod_result);
return 0;
}
Explanation:
- The program calculates the remainder of 5.3 divided by 2.0 using both
remainder()
andfmod()
. remainder()
rounds the quotient to the nearest integer whilefmod()
truncates the quotient.- The outputs are printed to illustrate the difference between the two functions.
Program Output:
Copied remainder(): -0.700000
Copied fmod(): 1.300000