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_powifor exact, efficient results. - Fractional exponent: uses the identity
x^p = exp(p * ln(x))viafixed_expandfixed_ln. - Special cases: handled directly with exact results.
§Domain
| Input condition | Result | Reason |
|---|---|---|
x = 0, p > 0 | Some(0) | Mathematical limit |
x = 0, p = 0 | None | Indeterminate form |
x = 0, p < 0 | None | Division by zero |
x < 0, p non-integer | None | Not real-valued |
x < 0, p integer | Some(x^p) | Real-valued, handled by fixed_powi |
p = 0 | Some(1) | x^0 = 1 for all non-zero x |
x = 1 | Some(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 successNonefor indeterminate or undefined inputs (see domain table above)Noneon 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