Exponents using the num::pow() function

So, I've been trying to find an easy way to do exponentiation in Rust. Did a search and found this:

Decided to experiment and wrote this:

use num_traits::pow;

fn main() {
    let base: u64 = 3;
    let exp: u64 = 17;
    let number = pow(base, exp);

    println!("{} raised to the {} power = {}", base, exp, number);

(I need this to work with all the numeric types, but decided to start with u64s.)
The compiler complains that num_traits is an "unresolved import" and that pow can't be found.

Anyway, I tried to follow the example given in the documentation, but it isn't working. How do I pull this function into scope?


If you added the num crate as dependency, you have to use use num::pow;, not use num_traits::pow;. The documentation refers to num_traits, as num re-exports this function from num_traits.

The num_traits crate is useful for doing this in generic code, but it's not necessary for simple cases like this. Integer types have a built-in pow method:

fn main() {
    let base: u64 = 3;
    let exp: u32 = 17; // NOTE: `pow` expects a 32-bit exponent
    let number = base.pow(exp);

    println!("{} raised to the {} power = {}", base, exp, number);

Well, this was just my first pick and trying to get something to work. I do need it to work generically. The numbers I will be sending to it can be either integers or floats. I just chose to use u64 as a place to start experimenting. Getting this to come into scope is my current problem. Once I can get that working, then I can experiment.

Run cargo add num-traits if you want to use num_traits::pow (or cargo add num to use num::pow, which is the same function re-exported by a different library).

Note that these pow functions can only take an unsigned integer (usize) as the exponent. You might also be interested in the various powf functions if you need floating-point exponents.

1 Like

Got it working. What I did was add

num-traits = "0.2"

in my Cargo.toml file. and change the exp variable to the usize type. It also worked when I passed an f64 to the pow function. Thanks for your help! Now I have to see if I can incorporate this into my project..... :nerd_face:

Would it be better to use the num crate instead of the num_traits crate? I'm not sure what is the difference between the two crates.

I just spent some time browsing crates.io to see if I could find an easy-to-use crate containing functions/methods that perform basic and common numeric operations. For instance, what I really need right now is a function that will round a float to a given decimal place. Haven't found that yet. I also need a function, accepting generic types, that will raise the passed value to a passed exponent. In the posts above you can see that I found a function, pow() in the num_traits crate that will do just that. I think this will work in my current project, but I anticipate that eventually this project is going to need to use exponents that contain decimals. Thus the requirement to use exponents of the usize type is eventually going to break down. Can anyone point me to a crate that contains that kind of math functions?

Says he.

And then immediately asks for something “extra-unusual” and “strange”.

It's because your whole idea doesn't make much sense, mathematically speaking.

It's actually quite unusual request even if newbies often think it should be common.

Rust, like most modern languages, uses binary floating points numbers, not decimal!

If you are doing financial calculations then your would need something like bigdecimal or rust_decimal. Not sure which is better but unless you are willing to purchase POWER machine it all would be emulated.

If you are not doing financial calculations then knowing just why you want to round to some decimal place would be nice.

The num crate is a convenience crate re-exporting everything of the num-* crates like num-traits, num-bigint, num-complex. If you only need num-traits, using that instead of the full num crate reduces the time required to build your project.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.