hi, i dont understand how works the function sqrt(), and pow.

can anyone give me an example please

# Help with functions sqrt()

From the docs:

sqrt:

```
use std::f32;
let positive = 4.0_f32;
let negative = -4.0_f32;
let abs_difference = (positive.sqrt() - 2.0).abs();
assert!(abs_difference <= f32::EPSILON);
assert!(negative.sqrt().is_nan());
```

```
let x: i32 = 2; // or any other integer type
assert_eq!(x.pow(5), 32);
```

Note I assume your confusion about `sqrt()`

stems from the fact not every numeric type has a `sqrt()`

method, only `f32`

and `f64`

If you’re looking for sqrt for integers, I made a crate for this: `integer-sqrt`

. However if you’re not sure what the whether you need integer sqrt, the answer is that you don’t and you should be using floats.

If you want to take the square root of an integer, just cast it to float and do the operation:

```
let i: i32 = 5;
println!("{}", (i as f64).sqrt());
```

and get 2.23606797749979 out.

The `num-integer`

crate has `Roots::sqrt()`

implemented for primitive integers, and `num-bigint`

implements this too. They offer more accuracy than a floating point mantissa allows, though the result is truncated. `f64`

will limit the initial value to 53 significant bits up front.

1 Like