Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/third_party/rust/num-rational/src/   (Firefox Browser Version 153.0.1©)  Datei vom 27.6.2026 mit Größe 5 kB image not shown  

Quelle  pow.rs   Sprache: Rust

 

use crate::Ratio;

use core::cmp;
use num_integer::Integer;
use num_traits::{One, Pow};

macro_rules! pow_unsigned_impl {
    (@ $exp:ty) => {
        type Output = Ratio<T>;
        #[inline]
        fn pow(self, expon: $exp) -> Ratio<T> {
            Ratio::new_raw(self.numer.pow(expon), self.denom.pow(expon))
        }
    };
    ($exp:ty) => {
        impl<T: Clone + Integer + Pow<$exp, Output = T>> Pow<$exp> for Ratio<T> {
            pow_unsigned_impl!(@ $exp);
        }
        impl<'a, T: Clone + Integer> Pow<$exp> for &'a Ratio<T>
        where
            &'a T: Pow<$exp, Output = T>,
        {
            pow_unsigned_impl!(@ $exp);
        }
        impl<'b, T: Clone + Integer + Pow<$exp, Output = T>> Pow<&'b $exp> for Ratio<T> {
            type Output = Ratio<T>;
            #[inline]
            fn pow(self, expon: &'b $exp) -> Ratio<T> {
                Pow::pow(self, *expon)
            }
        }
        impl<'a, 'b, T: Clone + Integer> Pow<&'b $exp> for &'a Ratio<T>
        where
            &'a T: Pow<$exp, Output = T>,
        {
            type Output = Ratio<T>;
            #[inline]
            fn pow(self, expon: &'b $exp) -> Ratio<T> {
                Pow::pow(self, *expon)
            }
        }
    };
}
pow_unsigned_impl!(u8);
pow_unsigned_impl!(u16);
pow_unsigned_impl!(u32);
pow_unsigned_impl!(u64);
pow_unsigned_impl!(u128);
pow_unsigned_impl!(usize);

macro_rules! pow_signed_impl {
    (@ &'b BigInt, BigUint) => {
        type Output = Ratio<T>;
        #[inline]
        fn pow(self, expon: &'b BigInt) -> Ratio<T> {
            match expon.sign() {
                Sign::NoSign => One::one(),
                Sign::Minus => {
                    Pow::pow(self, expon.magnitude()).into_recip()
                }
                Sign::Plus => Pow::pow(self, expon.magnitude()),
            }
        }
    };
    (@ $exp:ty, $unsigned:ty) => {
        type Output = Ratio<T>;
        #[inline]
        fn pow(self, expon: $exp) -> Ratio<T> {
            match expon.cmp(&0) {
                cmp::Ordering::Equal => One::one(),
                cmp::Ordering::Less => {
                    let expon = expon.wrapping_abs() as $unsigned;
                    Pow::pow(self, expon).into_recip()
                }
                cmp::Ordering::Greater => Pow::pow(self, expon as $unsigned),
            }
        }
    };
    ($exp:ty, $unsigned:ty) => {
        impl<T: Clone + Integer + Pow<$unsigned, Output = T>> Pow<$exp> for Ratio<T> {
            pow_signed_impl!(@ $exp, $unsigned);
        }
        impl<'a, T: Clone + Integer> Pow<$exp> for &'a Ratio<T>
        where
            &'a T: Pow<$unsigned, Output = T>,
        {
            pow_signed_impl!(@ $exp, $unsigned);
        }
        impl<'b, T: Clone + Integer + Pow<$unsigned, Output = T>> Pow<&'b $exp> for Ratio<T> {
            type Output = Ratio<T>;
            #[inline]
            fn pow(self, expon: &'b $exp) -> Ratio<T> {
                Pow::pow(self, *expon)
            }
        }
        impl<'a, 'b, T: Clone + Integer> Pow<&'b $exp> for &'a Ratio<T>
        where
            &'a T: Pow<$unsigned, Output = T>,
        {
            type Output = Ratio<T>;
            #[inline]
            fn pow(self, expon: &'b $exp) -> Ratio<T> {
                Pow::pow(self, *expon)
            }
        }
    };
}
pow_signed_impl!(i8, u8);
pow_signed_impl!(i16, u16);
pow_signed_impl!(i32, u32);
pow_signed_impl!(i64, u64);
pow_signed_impl!(i128, u128);
pow_signed_impl!(isize, usize);

#[cfg(feature = "num-bigint")]
mod bigint {
    use super::*;
    use num_bigint::{BigInt, BigUint, Sign};

    impl<T: Clone + Integer + for<'b> Pow<&'b BigUint, Output = T>> Pow<BigUint> for Ratio<T> {
        type Output = Ratio<T>;
        #[inline]
        fn pow(self, expon: BigUint) -> Ratio<T> {
            Pow::pow(self, &expon)
        }
    }
    impl<'a, T: Clone + Integer> Pow<BigUint> for &'a Ratio<T>
    where
        &'a T: for<'b> Pow<&'b BigUint, Output = T>,
    {
        type Output = Ratio<T>;
        #[inline]
        fn pow(self, expon: BigUint) -> Ratio<T> {
            Pow::pow(self, &expon)
        }
    }
    impl<'b, T: Clone + Integer + Pow<&'b BigUint, Output = T>> Pow<&'b BigUint> for Ratio<T> {
        pow_unsigned_impl!(@ &'b BigUint);
    }
    impl<'a, 'b, T: Clone + Integer> Pow<&'b BigUint> for &'a Ratio<T>
    where
        &'a T: Pow<&'b BigUint, Output = T>,
    {
        pow_unsigned_impl!(@ &'b BigUint);
    }

    impl<T: Clone + Integer + for<'b> Pow<&'b BigUint, Output = T>> Pow<BigInt> for Ratio<T> {
        type Output = Ratio<T>;
        #[inline]
        fn pow(self, expon: BigInt) -> Ratio<T> {
            Pow::pow(self, &expon)
        }
    }
    impl<'a, T: Clone + Integer> Pow<BigInt> for &'a Ratio<T>
    where
        &'a T: for<'b> Pow<&'b BigUint, Output = T>,
    {
        type Output = Ratio<T>;
        #[inline]
        fn pow(self, expon: BigInt) -> Ratio<T> {
            Pow::pow(self, &expon)
        }
    }
    impl<'b, T: Clone + Integer + Pow<&'b BigUint, Output = T>> Pow<&'b BigInt> for Ratio<T> {
        pow_signed_impl!(@ &'b BigInt, BigUint);
    }
    impl<'a, 'b, T: Clone + Integer> Pow<&'b BigInt> for &'a Ratio<T>
    where
        &'a T: Pow<&'b BigUint, Output = T>,
    {
        pow_signed_impl!(@ &'b BigInt, BigUint);
    }
}

Messung V0.5 in Prozent
C=94 H=93 G=93

¤ Dauer der Verarbeitung: 0.10 Sekunden  (vorverarbeitet am  2026-09-29) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

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.