fmod() Function

The fmod() function computes the floating-point remainder of a division, where the result is the difference between the dividend and the product of the divisor with the truncated quotient. This function is useful when you need to determine the leftover part after division for floating-point numbers.


Syntax of fmod()

</>
Copy
double fmod(double numer, double denom);
float  fmodf(float numer, float denom);
long double fmodl(long double numer, long double denom);

Parameters

ParameterDescription
numerDividend value for which the remainder is computed.
denomDivisor value used in the division.

It is important to note that the function returns the remainder with the same sign as the dividend. Also, if the divisor is zero, the behavior depends on the library implementation, possibly resulting in a domain error or returning zero.


Examples for fmod()

Example 1: Basic Usage of fmod() to Compute the Remainder

This example demonstrates how to use fmod() to compute the remainder of a division between two positive floating-point numbers.

Program

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

int main() {
    double dividend = 10.5;
    double divisor = 3.0;
    double result = fmod(dividend, divisor);

    printf("Remainder of %.2f / %.2f is %.2f\n", dividend, divisor, result);
    return 0;
}

Explanation:

  1. The program initializes a dividend of 10.5 and a divisor of 3.0.
  2. fmod() computes the remainder using the formula: remainder = dividend – (truncated quotient * divisor).
  3. The calculated remainder is stored in result.
  4. The remainder is then printed using printf().

Program Output:

Remainder of 10.50 / 3.00 is 1.50

Example 2: fmod() with Negative Dividend

This example illustrates the behavior of fmod() when the dividend is negative, showing that the remainder carries the sign of the dividend.

Program

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

int main() {
    double dividend = -10.5;
    double divisor = 3.0;
    double result = fmod(dividend, divisor);

    printf("Remainder of %.2f / %.2f is %.2f\n", dividend, divisor, result);
    return 0;
}

Explanation:

  1. The program sets a negative dividend (-10.5) while keeping the divisor positive (3.0).
  2. fmod() calculates the remainder, ensuring the sign of the result matches the dividend.
  3. The resulting remainder is stored in result.
  4. The output is then printed, demonstrating that the remainder is negative.

Program Output:

Remainder of -10.50 / 3.00 is -1.50

Example 3: Using fmodf() for Single-Precision Floating-Point Numbers

This example demonstrates how to use the single-precision variant fmodf() to compute the remainder with float values.

Program

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

int main() {
    float dividend = 7.25f;
    float divisor = 2.0f;
    float result = fmodf(dividend, divisor);

    printf("Remainder of %.2f / %.2f is %.2f\n", dividend, divisor, result);
    return 0;
}

Explanation:

  1. The program uses float variables for the dividend (7.25f) and divisor (2.0f).
  2. The function fmodf() computes the remainder for single-precision floats.
  3. The result is stored in the variable result.
  4. The remainder is printed using printf(), demonstrating the function’s operation with float values.

Program Output:

Remainder of 7.25 / 2.00 is 1.25