How to raise a number to a power?

48,354

Solution 1

Rust provides exponentiation via methods pow and checked_pow. The latter guards against overflows. Thus, to raise 2 to the power of 10, do:

let base: i32 = 2; // an explicit type is required
assert_eq!(base.pow(10), 1024);

The caret operator ^ is not used for exponentiation, it's the bitwise XOR operator.

Solution 2

Here is the simplest method which you can use:

let a = 2; // Can also explicitly define type i.e. i32
let a = i32::pow(a, 10);

It will output "2 raised to the power of 10", i.e.:

1024

Solution 3

For integers:

fn main() {
   let n = u32::pow(2, 10);
   println!("{}", n == 1024);
}

For floats:

fn main() {
   // example 1
   let f = f32::powf(2.0, 10.0);
   // example 2
   let g = f32::powi(2.0, 10);
   // print
   println!("{}", f == 1024.0 && g == 1024.0);
}

or, since your base is 2, you can also use shift:

fn main() {
   let n = 2 << 9;
   println!("{}", n == 1024);
}

Solution 4

I was trying the same thing as the OP. Thanks to the other answer authors.

Here's a variation that works for me:

let n = 2u32.pow(10);

This uses a literal unsigned 32 bit integer to set the type and base, then calls the pow() function on it.

Solution 5

Bit shifting is a good way to do this particular case:

assert_eq!(1 << 10, 1024);
Share:
48,354

Related videos on Youtube

Matthias Braun
Author by

Matthias Braun

Build a better Stack Exchange: Codidact All code I publish on Stack Overflow and other sites of the Stack Exchange network shall be licensed under the BSD 2-Clause License: Copyright Matthias Braun All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Updated on July 08, 2022

Comments

  • Matthias Braun
    Matthias Braun almost 2 years

    I was trying to raise an integer to a power using the caret operator (^), but I am getting surprising results, e.g.:

    assert_eq!(2^10, 8);
    

    How can I perform exponentiation in Rust?

  • Stargateur
    Stargateur almost 6 years
    is this optimized by 2 << 9 ?
  • ljedrz
    ljedrz almost 6 years
    @Stargateur In such cases the compiler optimizes the whole operation away, but it doesn't seem like it when the base is not known: godbolt.
  • unixia
    unixia over 2 years
    just tried this as a part of a constant and got the error that it is unstable for use in a constant. Putting it out in case someone else is trying the same. I'm on rustc 1.54.0 (a178d0322 2021-07-26)