View source code
Display the source code in std/math/algebraic.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

std.math.algebraic.hypot - multiple declarations

Function hypot

Calculates the length of the hypotenuse of a right-angled triangle with sides of length x and y. The hypotenuse is the value of the square root of the sums of the squares of x and y:

T hypot(T) (
  const T x,
  const T y
) pure nothrow @nogc @safe
if (isFloatingPoint!T);

sqrt(x2 + y2)

Note that hypot(x, y), hypot(y, x) and hypot(x, -y) are equivalent.

Special Values
x y hypot(x, y) invalid?
x ±0.0 |x| no
±∞ y +∞ no
±∞ NAN +∞ no

Example

import std.math.operations : feqrel;

assert(hypot(1.0, 1.0).feqrel(1.4142) > 16);
assert(hypot(3.0, 4.0).feqrel(5.0) > 16);
writeln(hypot(real.infinity, 1.0L)); // real.infinity
writeln(hypot(real.infinity, real.nan)); // real.infinity

Function hypot

Calculates the distance of the point (x, y, z) from the origin (0, 0, 0) in three-dimensional space. The distance is the value of the square root of the sums of the squares of x, y, and z:

T hypot(T) (
  const T x,
  const T y,
  const T z
) pure nothrow @nogc @safe
if (isFloatingPoint!T);

sqrt(x2 + y2 + z2)

Note that the distance between two points (x1, y1, z1) and (x2, y2, z2) in three-dimensional space can be calculated as hypot(x2-x1, y2-y1, z2-z1).

Parameters

NameDescription
x floating point value
y floating point value
z floating point value

Returns

The square root of the sum of the squares of the given arguments.

Example

import std.math.operations : isClose;

assert(isClose(hypot(1.0, 2.0, 2.0), 3.0));
assert(isClose(hypot(2.0, 3.0, 6.0), 7.0));
assert(isClose(hypot(1.0, 4.0, 8.0), 9.0));

Authors

Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger

License

Boost License 1.0.