Skip to main content

primitive_float_pow

Function primitive_float_pow 

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

Raises a primitive float to a primitive float power, returning a primitive float.

The result is correctly rounded to the nearest value, unlike f32::powf and f64::powf, which are not guaranteed to be correctly rounded.

$$ f(x,y) = x^y+\varepsilon. $$

  • If $x^y$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or assumed to be 0.
  • If $x^y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x^y|\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(x,\pm0.0)=1.0$ for any $x$, even NaN
  • $f(1.0,y)=1.0$ for any $y$, even NaN
  • $f(\text{NaN},y)=f(x,\text{NaN})=\text{NaN}$ otherwise
  • $f(x,\infty)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
  • $f(x,-\infty)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
  • $f(-1.0,\pm\infty)=1.0$
  • $f(-1.0,y)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
  • $f(\infty,y)=\infty$ if $y>0$, and $0.0$ if $y<0$
  • $f(-\infty,y)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive and not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is negative and not an odd integer
  • $f(0.0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$
  • $f(-0.0,y)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative and not an odd integer
  • $f(x,y)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer

If the result overflows, $\pm\infty$ is returned, and if it underflows, $\pm0.0$ is returned.

§Worst-case complexity

Constant time and additional memory.

§Examples

use malachite_base::num::float::NiceFloat;
use malachite_float::float::arithmetic::pow::primitive_float_pow;

assert_eq!(
    NiceFloat(primitive_float_pow(3.0, 2.5)),
    NiceFloat(15.588457268119896)
);
assert_eq!(
    NiceFloat(primitive_float_pow(2.0, 0.5)),
    NiceFloat(1.4142135623730951)
);
assert_eq!(
    NiceFloat(primitive_float_pow(10.0, -0.5)),
    NiceFloat(0.31622776601683794)
);