C Reference Β· ctype.h, math.h and others
C hypot(), cbrt() and fma() functions
Reference for C hypot(), cbrt(), fma(), copysign(), fmin() and fmax() from math.h: safe distances, cube roots, fused multiply-add and sign handling.
This page covers a handful of useful functions from <math.h> that were added in C99. hypot(x, y) returns the length of the hypotenuse, sqrt(x*x + y*y), without overflowing in the middle of the calculation. cbrt(x) is the cube root and, unlike pow(x, 1.0/3.0), works for negative numbers. fma(x, y, z) computes x*y + z with a single rounding. copysign(), fmin() and fmax() handle signs and extremes.
#include <math.h> double hypot(double x, double y); double cbrt(double x); double fma(double x, double y, double z);
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 hypot(double x, double y); double cbrt(double x); double fma(double x, double y, double z); double copysign(double x, double y); double fmax(double x, double y); double fmin(double x, double y);
Each has f and l variants (hypotf, cbrtl, fmaf, ...).
| Name | Description |
|---|---|
x, y |
Operands (the legs for hypot, the factors for fma). |
z |
The addend for fma. |
Returns: hypot: sqrt(xΒ² + yΒ²). cbrt: the real cube root, with the sign of x. fma: x*y + z rounded once. copysign: the magnitude of x with the sign of y. fmax/fmin: the larger or smaller; if one argument is NaN, the other is returned.
Errors / undefined behavior: none undefined. hypot returns an infinity with a range error when the true result is too large for double. hypot(x, Β±INFINITY) is INFINITY even if x is NaN. fma with an infinity times zero returns NaN.
Usage notes
- Why
hypot.x*xcan overflow or underflow even when the final length is representable.hypotscales internally, so use it for distances and vector lengths. It is slower than a plainsqrt, so avoid it when values are known to be moderate and speed matters, for instance by comparing squared distances. - Why
cbrt. The cube root is exact for perfect cubes on good libraries and handles negative input.pow(x, 1.0/3.0)returns NaN for negativex. fmais a single hardware instruction on modern CPUs, giving faster and more accurate polynomial evaluation, dot products and compensated sums. GCC may fusea*b + cby itself (-ffp-contract=fastis the default for GNU modes), so floating-point results can vary between compilers and flags; the standardSTDC FP_CONTRACTpragma controls this. Callingfmaexplicitly gives the single rounding, but without hardware support it is much slower.copysignis the reliable way to read or set the sign of a value, including-0.0and NaN. Usesignbit()to test the sign.fmax/fminignore NaN arguments where a comparison likea > b ? a : bwould not. They treat-0.0as smaller than+0.0only in some implementations, so do not rely on that.- Other C99 helpers:
fdim(positive difference),fabs,ldexp,frexp,modf,nextafter,erf,tgammaandlgamma.tgamma(5)is 24. - Standard version. All the functions on this page arrived in C99. C23 adds
fmaximum/fminimumwith strict NaN handling.