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()

</>
Copy
double remainder(double numer, double denom);

Parameters

ParameterDescription
numerValue of the numerator in the division.
denomValue 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

</>
Copy
#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:

  1. The program includes the necessary headers <stdio.h> and <math.h>.
  2. It computes the remainder of 5.3 divided by 2.0 using remainder().
  3. 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

</>
Copy
#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:

  1. The program computes the remainder for a negative numerator (-5.3) divided by a positive denominator (2.0).
  2. The function returns a remainder whose sign is influenced by the numerator.
  3. 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

</>
Copy
#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:

  1. The program calculates the remainder of 5.3 divided by 2.0 using both remainder() and fmod().
  2. remainder() rounds the quotient to the nearest integer while fmod() truncates the quotient.
  3. The outputs are printed to illustrate the difference between the two functions.

Program Output:

Copied remainder(): -0.700000
Copied fmod(): 1.300000