Skip to content
Tutorials Center
My learning

Topics

C Reference Β· ctype.h, math.h and others

C fabs(), fmod() and remainder() functions

Reference for C fabs(), fmod() and remainder() from math.h: absolute value and floating-point remainders, sign rules, abs() for integers and edge cases.

fabs() returns the absolute value of a floating-point number: the number without its sign. fmod() computes the floating-point remainder of a division, the equivalent of the % operator for double, which C's % does not accept. remainder() is a close relative that uses the nearest integer quotient instead of truncating.

For integers use abs() (for int), labs() (long) and llabs() (long long) from <stdlib.h>. fabs is the one for double, with fabsf and fabsl for float and long double.

C
#include <math.h>

double fabs(double x);
double fmod(double x, double y);
double remainder(double x, double y);

Try it

C fmod versus remainder

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Wrapping values, closeness and the zero divisor

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
double fabs(double x);        float fabsf(float x);        long double fabsl(long double x);
double fmod(double x, double y);
double remainder(double x, double y);
Name Description
x The value (fabs) or the dividend (fmod, remainder).
y The divisor.

Returns:

  • fabs(x): |x|. fabs(-0.0) is +0.0 and fabs(-INFINITY) is INFINITY.
  • fmod(x, y): x - n*y, where n is the quotient truncated toward zero. The result has the sign of x and a magnitude smaller than |y|.
  • remainder(x, y): x - n*y, where n is the quotient rounded to the nearest integer (ties to even). The result lies between -|y|/2 and +|y|/2.

Errors / undefined behavior: none undefined. fmod(x, 0) and remainder(x, 0) return NaN and signal a domain error, as do infinite x. fmod(x, Β±INFINITY) returns x for finite x.

Usage notes

  • No % for floating point. The operator works only on integers; use fmod.
  • Sign. fmod follows the sign of the dividend, like % in C99 and later. To wrap into [0, y), add y when the result is negative, as wrap_degrees does.
  • remainder is the IEEE 754 remainder and can return a negative number even when both arguments are positive. It is the right choice for range reduction in numeric code (for instance before computing a trigonometric function).
  • Exact. Unlike division, both remainders are computed exactly: the result has no rounding error, which makes fmod safe even for huge quotients.
  • Comparing floats. fabs(a - b) <= tolerance is the usual test for "close enough". Choose the tolerance for your data; a relative tolerance is better when magnitudes vary. See Floating-point comparison.
  • Integer absolute values. abs(INT_MIN) is undefined behavior because the result does not fit; check the argument first.
  • Wrong function. abs(-2.5) converts the argument to int and returns 2. Use fabs for floating-point values and enable -Wconversion or -Wall to be warned.
  • Type-generic. With <tgmath.h>, fabs picks the right variant by argument type.
  • Standard version. fabs and fmod in C89, remainder and the f/l variants in C99.