Skip to content

Int::pow doesn't handle overflow #22506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
brson opened this issue Feb 18, 2015 · 2 comments
Closed

Int::pow doesn't handle overflow #22506

brson opened this issue Feb 18, 2015 · 2 comments

Comments

@brson
Copy link
Contributor

brson commented Feb 18, 2015

It takes some large exponents and the overflow behavior is not documented nor coded:

    /// Raises self to the power of `exp`, using exponentiation by squaring.                                                                                                                                                      
    ///                                                                                                                                                                                                                           
    /// # Example                                                                                                                                                                                                                 
    ///                                                                                                                                                                                                                           
    /// ```rust                                                                                                                                                                                                                   
    /// use std::num::Int;                                                                                                                                                                                                        
    ///                                                                                                                                                                                                                           
    /// assert_eq!(2.pow(4), 16);                                                                                                                                                                                                 
    /// ```                                                                                                                                                                                                                       
    #[unstable(feature = "core",                                                                                                                                                                                                  
               reason = "pending integer conventions")]
    #[inline]
    fn pow(self, mut exp: uint) -> Self {                                                                        
        let mut base = self;                                                                                     
        let mut acc: Self = Int::one();                                                                          
        while exp > 0 {                                                                                          
            if (exp & 1) == 1 {                                                                                  
                acc = acc * base;                                                                                
            }
            base = base * base;                                                                                  
            exp /= 2;                                                                                            
        }
        acc
    }
@brson brson added the A-libs label Feb 18, 2015
@brson
Copy link
Contributor Author

brson commented Feb 18, 2015

@GuillaumeGomez will work on this.

@GuillaumeGomez
Copy link
Member

Should be closed since it has been fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants