pow_impl!(f32, i8, i32, <f32 as Float>::powi);
pow_impl!(f32, u8, i32, <f32 as Float>::powi);
pow_impl!(f32, i16, i32, <f32 as Float>::powi);
pow_impl!(f32, u16, i32, <f32 as Float>::powi);
pow_impl!(f32, i32, i32, <f32 as Float>::powi);
pow_impl!(f64, i8, i32, <f64 as Float>::powi);
pow_impl!(f64, u8, i32, <f64 as Float>::powi);
pow_impl!(f64, i16, i32, <f64 as Float>::powi);
pow_impl!(f64, u16, i32, <f64 as Float>::powi);
pow_impl!(f64, i32, i32, <f64 as Float>::powi);
pow_impl!(f32, f32, f32, <f32 as Float>::powf);
pow_impl!(f64, f32, f64, <f64 as Float>::powf);
pow_impl!(f64, f64, f64, <f64 as Float>::powf);
}
/// Raises a value to the power of exp, using exponentiation by squaring. /// /// Note that `0⁰` (`pow(0, 0)`) returns `1`. Mathematically this is undefined. /// /// # Example /// /// ```rust /// use num_traits::pow; /// /// assert_eq!(pow(2i8, 4), 16); /// assert_eq!(pow(6u8, 3), 216); /// assert_eq!(pow(0u8, 0), 1); // Be aware if this case affects you /// ``` #[inline] pubfn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) -> T { if exp == 0 { return T::one();
}
while exp & 1 == 0 {
base = base.clone() * base;
exp >>= 1;
} if exp == 1 { return base;
}
/// Raises a value to the power of exp, returning `None` if an overflow occurred. /// /// Note that `0⁰` (`checked_pow(0, 0)`) returns `Some(1)`. Mathematically this is undefined. /// /// Otherwise same as the `pow` function. /// /// # Example /// /// ```rust /// use num_traits::checked_pow; /// /// assert_eq!(checked_pow(2i8, 4), Some(16)); /// assert_eq!(checked_pow(7i8, 8), None); /// assert_eq!(checked_pow(7u32, 8), Some(5_764_801)); /// assert_eq!(checked_pow(0u32, 0), Some(1)); // Be aware if this case affect you /// ``` #[inline] pubfn checked_pow<T: Clone + One + CheckedMul>(mut base: T, mut exp: usize) -> Option<T> { if exp == 0 { return Some(T::one());
}
while exp & 1 == 0 {
base = base.checked_mul(&base)?;
exp >>= 1;
} if exp == 1 { return Some(base);
}
letmut acc = base.clone(); while exp > 1 {
exp >>= 1;
base = base.checked_mul(&base)?; if exp & 1 == 1 {
acc = acc.checked_mul(&base)?;
}
}
Some(acc)
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.9 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.