hypot()

NAME

hypot(), cabs(), z_abs() - euclidean distance and complex absolute value functions

SYNOPSIS

#include <math.h>

double hypot (double x, double y) struct complex {double x, y;} z; double cabs (z) double z_abs (struct z *z)

DESCRIPTION

The hypot(3), cabs(3), and z_abs(3) functions compute the sqrt(x*x+y*y) in such a way that underflow will not happen, and overflow occurs only if the final result deserves it.

hypot(Infinity,v) = hypot(v,Infinity) = +Infinity for all v, including NaN.

The z_abs(3) function is similar to cabs(3) except that it takes a pointer to a struct z rather than the structure itself.

ERROR (due to Roundoff, etc.)

Below 0.97 ulps. Consequently hypot(5.0,12.0) = 13.0 exactly; in general, hypot() and cabs() return an integer whenever an integer might be expected.

NOTES

As might be expected, hypot(v, NaN) and hypot(NaN, v) are NaN for all finite v; with "reserved operand" in place of "NaN". But programmers might be surprised at first to discover that hypot(+-Infinity, NaN) = +Infinity. This is intentional; it happens because hypot(Infinity, v) = +Infinity for all v, finite or infinite. Hence hypot(Infinity, v) is independent of v. The IEEE NaN is designed to disappear when it turns out to be irrelevant, as it does in hypot(Infinity, NaN).

Given the following:

#include <math.h>
double a, b, r;
complex z;
a = 2.0;
b = 3.0;
z.x = 2.0;
z.y = 3.0;

The following give identical values for r:

r = hypot(a, b);
r = cabs(z);
r = z_abs(&z);

RETURN VALUE

On success, hypot(3), cabs(3), and z_abs(3) return the length of the hypotenuse of a right-angled triangle with sides of length x and y.

If the result would cause overflow, they return NaN and set errno. If x or y is NaN, they return NaN and set errno.

ERRORS

These functions can fail for the following reasons:

[EDOM]
One of the arguments is NaN.
[ERANGE]
The result would cause overflow (in which case HUGE_VAL is returned) or underflow (in which case 0 is returned).

SEE ALSO

math(3)

sqrt(3)