C Library Functions -fmod()

C standard library - <math.h>

Description

C Library Functionsdouble fmod(double x, double y)returnxdivided byythe remainder.

fmod()is the C standard library<math.h>a function used to calculate the remainder of dividing two floating-point numbers. This function returns the remainder of the division operation, with the same sign as the dividend.

Declaration

Below is the declaration of the fmod() function.

double fmod(double x, double y)

Parameter

  • x: dividend, a floating-point number.
  • y: divisor, a floating-point number.

Return Value

  • returnxdivided byythe remainder of, whose sign isxSame.
  • ifyis 0, the result is undefined, and may produce a calculation error.

Example

The following example demonstrates the usage of the fmod() function.

Example

#include <stdio.h>
#include <math.h>

int main ()
{
   float a, b;
   int c;
   a = 9.2;
   b = 3.7;
   c = 2;
   printf("%f / %d remainder is %lf\n", a, c, fmod(a,c));
   printf("%f / %f remainder is %lf\n", a, b, fmod(a,b));
   
   return(0);
}

Let us compile and run the above program, which will produce the following result:

9.200000 / 2 的余数是 1.200000
9.200000 / 3.700000 的余数是 1.800000

Remainder calculation for multiple values

The following example demonstrates how to handle remainder calculations for multiple values:

Example

#include <stdio.h>
#include <math.h>

int main() {
    double numerators[] = {5.3, 7.1, -4.2, 10.0};
    double denominators[] = {2.0, 3.0, 2.5, -3.5};
    int num_values = sizeof(numerators) / sizeof(numerators[0]);

    for (int i = 0; i < num_values; i++) {
        double x = numerators[i];
        double y = denominators[i];
        double result = fmod(x, y);

        printf("fmod(%f, %f) = %f\n", x, y, result);
    }

    return 0;
}

Let us compile and run the above program, which will produce the following result:

fmod(5.300000, 2.000000) = 1.300000
fmod(7.100000, 3.000000) = 1.100000
fmod(-4.200000, 2.500000) = -1.700000
fmod(10.000000, -3.500000) = 3.000000

Code explanation

  • Define two arrays containing multiple floating-point numbersnumeratorsanddenominators, representing the dividend and divisor respectively.
  • UsageforLoop through each pair of dividend and divisor, callingfmod(x, y)function performs remainder calculation.
  • Prints the result of calculating the remainder for each pair of values.

Use Case

fmod()The function has a wide range of applications in many fields, including but not limited to:

  • Calculate the remainder of floating-point numbers for simulating periodic phenomena.
  • Implement cyclic operations, such as periodic calculations of angles.
  • Handle coordinate transformations and boundary conditions in graphics and game programming.

Summary

fmod()The function is used to calculate the remainder of dividing two floating-point numbers, making it an important tool for handling floating-point remainder calculations. Through proper use offmod(), accurate handling of floating-point remainders can be achieved in scientific computing, engineering applications, and graphics programming.

C standard library - <math.h>

other extensions