Skip to main content

primitive_float_tanh

Function primitive_float_tanh 

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

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

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

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

Neither overflow nor underflow is possible. The result is subnormal only when $x$ is, and then it is $x$ itself.

§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::tanh::primitive_float_tanh;

assert!(primitive_float_tanh(f32::NAN).is_nan());
assert_eq!(
    NiceFloat(primitive_float_tanh(f32::INFINITY)),
    NiceFloat(1.0)
);
assert_eq!(
    NiceFloat(primitive_float_tanh(f32::NEGATIVE_INFINITY)),
    NiceFloat(-1.0)
);
assert_eq!(NiceFloat(primitive_float_tanh(-0.0f32)), NiceFloat(-0.0));
assert_eq!(
    NiceFloat(primitive_float_tanh(1.0f32)),
    NiceFloat(0.7615942)
);
assert_eq!(
    NiceFloat(primitive_float_tanh(-1.0f64)),
    NiceFloat(-0.7615941559557649)
);
assert_eq!(NiceFloat(primitive_float_tanh(20.0f64)), NiceFloat(1.0));