pub fn powi(self, n: i32) -> Self {
        let mut base = self;
        let mut abs_n = i32::abs(n);
        let mut result = 1.0;

        if n < 0 {
            base = 1.0 / self;
        }

        if n == 0 {
            return Self::ONE;
        }

        // 0.0 == 0.0 and -0.0 according to IEEE standards.
        if self == Self::ZERO && n > 0 {
            return self;
        }

        // For values less than 2.0, but greater than 0.5 (1.0/2.0), you can multiply longer without
        // going over exponent, i.e. 1.1 multiplied against itself will grow slowly.
        if !(0.5..2.0).contains(&self.abs().0) {
            // Approximation if we end up outside of the range of floating point values,
            // then we end early
            let approx_final_exponent = self.extract_exponent_value() * n;
            let max_representable_exponent = 127;
            let min_representable_exponent = -126 - (MANTISSA_BITS as i32);
            if approx_final_exponent > max_representable_exponent
                || (self == Self::ZERO && approx_final_exponent < 0)
            {
                if self.is_sign_positive() || n & 1 == 0 {
                    return Self::INFINITY;
                } else {
                    return Self::NEG_INFINITY;
                }
            } else if approx_final_exponent < min_representable_exponent {
                // We may want to copy the sign and do the same thing as above,
                // but that seems like an awful amount of work when 99.99999% of people only care
                // about bare zero
                return Self::ZERO;
            }
        }

        loop {
            if (abs_n & 1) == 1 {
                result *= base;
            }

            abs_n >>= 1;

            if abs_n == 0 {
                return Self(result);
            }

            base *= base;
        }
    }
/// Approximates a number raised to an integer power.
Approximates a number raised to an integer power.
Approximates a number raised to an integer power .
pub fn powi(self, n: i32) -> Self {
        let mut base = self;
        let mut abs_n = i32::abs(n);
        let mut result = 1.0;
        if n < 0 {
            base = 1.0 / self;
        }
        if n == 0 {
            return Self::ONE;
        }
        // 0.0 == 0.0 and -0.0 according to IEEE standards.
        if self == Self::ZERO && n > 0 {
            return self;
        }
        // For values less than 2.0, but greater than 0.5 (1.0/2.0), you can multiply longer without
        // going over exponent, i.e. 1.1 multiplied against itself will grow slowly.
        if !(0.5..2.0).contains(&self.abs().0) {
            // Approximation if we end up outside of the range of floating point values,
            // then we end early
            let approx_final_exponent = self.extract_exponent_value() * n;
            let max_representable_exponent = 127;
            let min_representable_exponent = -126 - (MANTISSA_BITS as i32);
            if approx_final_exponent > max_representable_exponent
                || (self == Self::ZERO && approx_final_exponent < 0)
            {
                if self.is_sign_positive() || n & 1 == 0 {
                    return Self::INFINITY;
                } else {
                    return Self::NEG_INFINITY;
                }
            } else if approx_final_exponent < min_representable_exponent {
                // We may want to copy the sign and do the same thing as above,
                // but that seems like an awful amount of work when 99.99999% of people only care
                // about bare zero
                return Self::ZERO;
            }
        }
        loop {
            if (abs_n & 1) == 1 {
                result *= base;
            }
            abs_n >>= 1;
            if abs_n == 0 {
                return Self(result);
            }
            base *= base;
        }
    }
pub fn powi ( self , n : i32 ) -> Self { let mut base = self ; let mut abs_n = i32 :: abs ( n ) ; let mut result = 1.0 ; if n < 0 { base = 1.0 / self ; } if n == 0 { return Self :: ONE ; } // 0.0 == 0.0 and -0.0 according to IEEE standards. if self == Self :: ZERO && n > 0 { return self ; } // For values less than 2.0, but greater than 0.5 (1.0/2.0), you can multiply longer without // going over exponent, i.e. 1.1 multiplied against itself will grow slowly. if ! ( 0.5 .. 2.0 ) . contains ( & self . abs ( ) . 0 ) { // Approximation if we end up outside of the range of floating point values, // then we end early let approx_final_exponent = self . extract_exponent_value ( ) * n ; let max_representable_exponent = 127 ; let min_representable_exponent = - 126 - ( MANTISSA_BITS as i32 ) ; if approx_final_exponent > max_representable_exponent || ( self == Self :: ZERO && approx_final_exponent < 0 ) { if self . is_sign_positive ( ) || n & 1 == 0 { return Self :: INFINITY ; } else { return Self :: NEG_INFINITY ; } } else if approx_final_exponent < min_representable_exponent { // We may want to copy the sign and do the same thing as above, // but that seems like an awful amount of work when 99.99999% of people only care // about bare zero return Self :: ZERO ; } } loop { if ( abs_n & 1 ) == 1 { result *= base ; } abs_n >>= 1 ; if abs_n == 0 { return Self ( result ) ; } base *= base ; } }
Approximates a number raised to an integer power.
Approximates a number raised to an integer power .
self
n
i32
self
n
i32