Function fixed_pow

Source
fn fixed_pow<T>(x: &T, p: &T) -> Option<T>
where T: FixedPointNumber + Copy + PartialOrd + FixedPointInfo, T::Inner: From<u8> + Shr<u32, Output = T::Inner> + TryInto<i128> + Copy,
Expand description

Computes x^p for fixed-point numbers.

§Algorithm

Three computation paths depending on the inputs:

  • Integer exponent: uses binary exponentiation via fixed_powi for exact, efficient results.
  • Fractional exponent: uses the identity x^p = exp(p * ln(x)) via fixed_exp and fixed_ln.
  • Special cases: handled directly with exact results.

§Domain

Input conditionResultReason
x = 0, p > 0Some(0)Mathematical limit
x = 0, p = 0NoneIndeterminate form
x = 0, p < 0NoneDivision by zero
x < 0, p non-integerNoneNot real-valued
x < 0, p integerSome(x^p)Real-valued, handled by fixed_powi
p = 0Some(1)x^0 = 1 for all non-zero x
x = 1Some(1)1^p = 1 for all p

§Arguments

  • x - The base as a fixed-point number.
  • p - The exponent as a fixed-point number.

§Returns

  • Some(x^p) on success
  • None for indeterminate or undefined inputs (see domain table above)
  • None on overflow

§Examples

// Integer exponent
let x = FixedU64::saturating_from_integer(2);
let p = FixedU64::saturating_from_integer(3);
assert_eq!(fixed_pow(&x, &p), Some(FixedU64::saturating_from_integer(8)));

// Fractional exponent
let x = FixedU64::saturating_from_integer(4);
let p = FixedU64::saturating_from_rational(1, 2);
let result = fixed_pow(&x, &p).unwrap();
// result ~= 2.0 (square root of 4)

// Undefined cases
let zero = FixedU64::zero();
assert_eq!(fixed_pow(&zero, &zero), None); // 0^0 indeterminate