-
Notifications
You must be signed in to change notification settings - Fork 2
/
powx-n.rs
52 lines (43 loc) · 1.06 KB
/
powx-n.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#![allow(dead_code, unused, unused_variables)]
fn main() {
println!("{}", Solution::my_pow(0.00001, 2147483647));
println!("{}", Solution::my_pow(2.00000, 10));
println!("{}", Solution::my_pow(2.00000, /**/ -2));
println!("{}", Solution::my_pow(2.00000, -2147483648));
}
struct Solution;
impl Solution {
pub fn my_pow(x: f64, n: i32) -> f64 {
if x == 0f64 {
return 0f64;
}
if n == 0 {
return 1f64;
}
let mut n = n;
let flag = n < 0;
let mut result = x;
let mut n1 = 2i32;
let s = n == std::i32::MIN;
if s {
n += 1;
}
while n.abs() > 1 {
result *= result;
let n2 = n1.overflowing_mul(2);
if n2.1 || n2.0 > n.abs() {
result *= Self::my_pow(x, n.abs() - n1);
break;
}
n1 = n2.0;
}
if s {
result *= x
}
if flag {
1f64 / result
} else {
result
}
}
}