Skip to main content

primitive_float_pow_rational

Function primitive_float_pow_rational 

Source
pub fn primitive_float_pow_rational<T>(x: T, y: &Rational) -> 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 Rational power, returning a primitive float.

The result is correctly rounded to the nearest value. Unlike a primitive-float exponent, the exact Rational exponent selects a definite branch of the power, so results that are exactly representable (such as roots of perfect powers) come out exactly.

$$ 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,0)=1.0$ for any $x$, even NaN
  • $f(1.0,y)=1.0$
  • $f(\text{NaN},y)=\text{NaN}$ if $y \neq 0$
  • $f(x,y)=\text{NaN}$ if $x<0$ and $y$ is not an integer
  • $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

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_rational;
use malachite_q::Rational;

assert_eq!(
    NiceFloat(primitive_float_pow_rational(
        4.0,
        &Rational::from_signeds(1, 2)
    )),
    NiceFloat(2.0)
);
assert_eq!(
    NiceFloat(primitive_float_pow_rational(
        2.0,
        &Rational::from_signeds(3, 2)
    )),
    NiceFloat(2.8284271247461903)
);
assert_eq!(
    NiceFloat(primitive_float_pow_rational(
        4.0,
        &Rational::from_signeds(-1, 2)
    )),
    NiceFloat(0.5)
);
assert!(primitive_float_pow_rational::<f64>(-8.0, &Rational::from_signeds(1, 3)).is_nan());