Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

fmod — C23 mathematical function

`fmod(x, y)` from `<math.h>` returns `x - n*y`, where `n` is `x/y` truncated toward zero; the result magnitude is less than `|y|` and its sign follows `x`.

Version: C23

Syntax

double fmod(double x, double y);

fmod(x, y) from <math.h> returns x - n*y, where n is x/y truncated toward zero; the result magnitude is less than |y| and its sign follows x.

The example calls fmod(-17.5, 5.0) and fmod(17.5, 5.0). Output -2.5 2.5 demonstrates the dividend sign and the symmetry absent from a remainder based on rounding the quotient to nearest.

y == 0 or infinite x causes a domain error; the base-C return is then implementation-defined. Do not confuse it with remainder, which chooses a different quotient. Annex F preserves finite x when y is infinite and specifies signed-zero and NaN behavior.

Examples

#include <math.h>
#include <stdio.h>
int main(void) { printf("%.1f %.1f\n", fmod(-17.5,5.0), fmod(17.5,5.0)); return 0; }

Result

-2.5 2.5

The example calls `fmod(-17.5, 5.0)` and `fmod(17.5, 5.0)`. Output `-2.5 2.5` demonstrates the dividend sign and the symmetry absent from a remainder based on rounding the quotient to nearest.

Sources