Skip to content
Tutorials Center
My learning

Topics

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.

C
#include <math.h>

double hypot(double x, double y);
double cbrt(double x);
double fma(double x, double y, double z);

Try it

C Safe distances and cube roots

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 fma and Horner's method

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 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*x can overflow or underflow even when the final length is representable. hypot scales internally, so use it for distances and vector lengths. It is slower than a plain sqrt, 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 negative x.
  • fma is a single hardware instruction on modern CPUs, giving faster and more accurate polynomial evaluation, dot products and compensated sums. GCC may fuse a*b + c by itself (-ffp-contract=fast is the default for GNU modes), so floating-point results can vary between compilers and flags; the standard STDC FP_CONTRACT pragma controls this. Calling fma explicitly gives the single rounding, but without hardware support it is much slower.
  • copysign is the reliable way to read or set the sign of a value, including -0.0 and NaN. Use signbit() to test the sign.
  • fmax/fmin ignore NaN arguments where a comparison like a > b ? a : b would not. They treat -0.0 as smaller than +0.0 only in some implementations, so do not rely on that.
  • Other C99 helpers: fdim (positive difference), fabs, ldexp, frexp, modf, nextafter, erf, tgamma and lgamma. tgamma(5) is 24.
  • Standard version. All the functions on this page arrived in C99. C23 adds fmaximum/fminimum with strict NaN handling.