Skip to main content

primitive_float_pow_integer

Function primitive_float_pow_integer 

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

Raises a primitive float to the power of an Integer, returning a primitive float.

The result is correctly rounded to the nearest value. Unlike a primitive-float exponent, an arbitrarily large Integer exponent is handled exactly.

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

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

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_integer;
use malachite_nz::integer::Integer;

assert_eq!(
    NiceFloat(primitive_float_pow_integer(3.0, &Integer::from(5))),
    NiceFloat(243.0)
);
assert_eq!(
    NiceFloat(primitive_float_pow_integer(2.0, &Integer::from(-3))),
    NiceFloat(0.125)
);
assert_eq!(
    NiceFloat(primitive_float_pow_integer(-2.0, &Integer::from(3))),
    NiceFloat(-8.0)
);