fmodf Function

Calculates the remainder of the division between two single-precision floating-point values.

Include

<math.h>

Prototype

float fmodf(float x, float y);

Arguments

x
a double precision floating-point value
y
a double precision floating-point value

Return Value

Returns the remainder of x/y with the same sign as x and magnitude less than the magnitude of y, or returns NaN if y is zero.

Remarks

If y is zero, a domain error occurs, and errno will be set to EDOM.

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

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

int main(void)
{
  float x,y,z;

  errno = 0;
  x = 7.0F;
  y = 3.0F;
  z = fmodf(x, y);
  if(errno)
    perror("Error");
  printf("For fmodf(%f, %f) the remainder is"
          " %f\n\n", x, y, z);

  errno = 0;
  x = -5.0F;
  y = 3.0F;
  z = fmodf(x, y);
  if(errno)
    perror("Error");
     printf("For fmodf(%f, %f) the remainder is %f\n", x, y, z);

  errno = 0;
  x = 5.0F;
  y = -3.0F;
  z = fmodf(x, y);
  if(errno)
    perror("Error");
  printf("For fmodf(%f, %f) the remainder is %f\n", x, y, z);

  errno = 0;
  x = 5.0F;
  y = -5.0F;
  z = fmodf (x, y);
  if(errno)
    perror("Error");
  printf("For fmodf (%f, %f) the remainder is %f\n", x, y, z);

  errno = 0;
  x = 7.0F;
  y = 0.0F;
  z = fmodf(x, y);
  if(errno)
    perror("Error");
  printf("For fmodf(%f, %f) the remainder is %f\n", x, y, z);

  errno = 0;
  x = 7.0F;
  y = 7.0F;
  z = fmodf(x, y);
  if(errno)
    perror("Error");
  printf("For fmodf(%f, %f) the remainder is %f\n", x, y, z);
}

Example Output

For fmodf (7.000000, 3.000000) the remainder is 1.000000
For fmodf (-5.000000, 3.000000) the remainder is -2.000000
For fmodf (5.000000, -3.000000) the remainder is 2.000000
For fmodf (5.000000, -5.000000) the remainder is 0.000000
Error: domain error
For fmodf (7.000000, 0.000000) the remainder is nan
For fmodf (7.000000, 7.000000) the remainder is 0.000000