Skip to main content

primitive_float_sech

Function primitive_float_sech 

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

Computes $\operatorname{sech} x$, the hyperbolic secant of a primitive float. The result is correctly rounded.

$$ f(x) = \operatorname{sech} x+\varepsilon. $$

  • If $\operatorname{sech} x$ is zero or NaN, $\varepsilon$ may be ignored or assumed to be 0.
  • If $\operatorname{sech} x$ is nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 \operatorname{sech} x\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(\text{NaN})=\text{NaN}$
  • $f(\pm\infty)=0.0$
  • $f(\pm0.0)=1.0$

Overflow is not possible, since the result lies in $[0, 1]$. An x of large magnitude gives a subnormal result, or underflows to 0.0.

§Worst-case complexity

Constant time and additional memory.

§Examples

use malachite_base::num::float::NiceFloat;
use malachite_float::float::arithmetic::sech::primitive_float_sech;

assert!(primitive_float_sech(f32::NAN).is_nan());
assert_eq!(
    NiceFloat(primitive_float_sech(f32::INFINITY)),
    NiceFloat(0.0)
);
assert_eq!(NiceFloat(primitive_float_sech(-0.0f32)), NiceFloat(1.0));
assert_eq!(
    NiceFloat(primitive_float_sech(1.0f32)),
    NiceFloat(0.6480543)
);
assert_eq!(
    NiceFloat(primitive_float_sech(-1.0f64)),
    NiceFloat(0.6480542736638853)
);
assert_eq!(
    NiceFloat(primitive_float_sech(720.0f64)),
    NiceFloat(4.06446160484e-313)
);
assert_eq!(NiceFloat(primitive_float_sech(746.0f64)), NiceFloat(0.0));