Skip to main content

primitive_float_exp

Function primitive_float_exp 

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

Computes $e^x$, the exponential of a primitive float. Using this function is more accurate than using the default exp function or the one provided by libm.

$$ f(x) = e^x+\varepsilon. $$

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

Overflow and underflow are possible: a large positive x gives $\infty$, and a large negative x gives 0.0.

§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::exp::primitive_float_exp;

assert!(primitive_float_exp(f32::NAN).is_nan());
assert_eq!(
    NiceFloat(primitive_float_exp(f32::INFINITY)),
    NiceFloat(f32::INFINITY)
);
assert_eq!(
    NiceFloat(primitive_float_exp(f32::NEGATIVE_INFINITY)),
    NiceFloat(0.0)
);
assert_eq!(NiceFloat(primitive_float_exp(0.0f32)), NiceFloat(1.0));
assert_eq!(NiceFloat(primitive_float_exp(1.0f32)), NiceFloat(2.7182817));