Skip to main content

primitive_float_hypot

Function primitive_float_hypot 

Source
pub fn primitive_float_hypot<T>(x: T, y: T) -> T
where Float: From<T> + PartialOrd<T>, for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float> + PrimitiveFloat,
Expand description

Computes the hypotenuse of two primitive floats, $\sqrt{x^2+y^2}$, with a single rounding.

$$ f(x,y) = \sqrt{x^2+y^2}+\varepsilon. $$

  • If $\sqrt{x^2+y^2}$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or assumed to be 0.
  • If $\sqrt{x^2+y^2}$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2 \sqrt{x^2+y^2}\rfloor-p}$, where $p$ is the precision of the output (typically 24 if T is a f32 and 53 if T is a f64, but less if the output is subnormal).

Special cases:

  • $f(\pm\infty,x)=f(x,\pm\infty)=\infty$, even when the other argument is NaN
  • $f(\text{NaN},x)=f(x,\text{NaN})=\text{NaN}$ if $x$ is not infinite
  • $f(\pm0.0,\pm0.0)=0.0$

The result is never negative, and a zero result is always positive.

§Worst-case complexity

Constant time and additional memory.

§Examples

use core::f64::consts::{E, PI};
use malachite_base::num::float::NiceFloat;
use malachite_float::float::arithmetic::hypot::primitive_float_hypot;

assert_eq!(
    NiceFloat(primitive_float_hypot(PI, E)),
    NiceFloat(4.154354402313313)
);