Skip to content

Int::pow doesn't handle overflow #22506

Closed
@brson

Description

@brson

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
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions