Skip to main content

primitive_float_cosh

Function primitive_float_cosh 

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

Computes $\cosh x$, the hyperbolic cosine of a primitive float. The result is correctly rounded.

$$ f(x) = \cosh x+\varepsilon. $$

  • If $\cosh x$ is infinite or NaN, $\varepsilon$ may be ignored or assumed to be 0.
  • If $\cosh x$ is finite, then $|\varepsilon| < 2^{\lfloor\log_2 \cosh x\rfloor-p}$, where $p$ is the precision of the output (24 if T is a f32 and 53 if T is a f64).

Special cases:

  • $f(\text{NaN})=\text{NaN}$
  • $f(\infty)=\infty$
  • $f(-\infty)=\infty$
  • $f(\pm0.0)=1.0$

Overflow is possible: an x of large magnitude gives $\infty$. Since $\cosh x\geq 1$, the result never underflows.

§Worst-case complexity

Constant time and additional memory.

§Examples

use malachite_base::num::basic::traits::NegativeInfinity;
use malachite_base::num::float::NiceFloat;
use malachite_float::float::arithmetic::cosh::primitive_float_cosh;

assert!(primitive_float_cosh(f32::NAN).is_nan());
assert_eq!(
    NiceFloat(primitive_float_cosh(f32::INFINITY)),
    NiceFloat(f32::INFINITY)
);
assert_eq!(
    NiceFloat(primitive_float_cosh(f32::NEGATIVE_INFINITY)),
    NiceFloat(f32::INFINITY)
);
assert_eq!(NiceFloat(primitive_float_cosh(0.0f32)), NiceFloat(1.0));
assert_eq!(
    NiceFloat(primitive_float_cosh(1.0f32)),
    NiceFloat(1.5430807)
);
assert_eq!(
    NiceFloat(primitive_float_cosh(-1.0f32)),
    NiceFloat(1.5430807)
);
assert_eq!(
    NiceFloat(primitive_float_cosh(100.0f32)),
    NiceFloat(f32::INFINITY)
);