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.
#include <math.h> double fabs(double x); double fmod(double x, double y); double remainder(double x, double y);
Try it
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.
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
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.0andfabs(-INFINITY)isINFINITY.fmod(x, y):x - n*y, wherenis the quotient truncated toward zero. The result has the sign ofxand a magnitude smaller than|y|.remainder(x, y):x - n*y, wherenis the quotient rounded to the nearest integer (ties to even). The result lies between-|y|/2and+|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; usefmod. - Sign.
fmodfollows the sign of the dividend, like%in C99 and later. To wrap into[0, y), addywhen the result is negative, aswrap_degreesdoes. remainderis 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
fmodsafe even for huge quotients. - Comparing floats.
fabs(a - b) <= toleranceis 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 tointand returns 2. Usefabsfor floating-point values and enable-Wconversionor-Wallto be warned. - Type-generic. With
<tgmath.h>,fabspicks the right variant by argument type. - Standard version.
fabsandfmodin C89,remainderand thef/lvariants in C99.