Created
January 3, 2019 13:51
-
-
Save Hanaasagi/cf69bf39d8e8b4a3f21a3bf2cd287ea0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn my_sqrt(x: i32) -> i32 { | |
let mut n = x as f64; | |
let half = 0.5 * n; | |
let mut i = unsafe { | |
std::mem::transmute::<f64, i64>(n) | |
}; | |
i = 0x5fe6ec85e7de30da - (i>>1); | |
n = unsafe{ | |
std::mem::transmute::<i64, f64>(i) | |
}; | |
for i in 0..3 { | |
n = n*(1.5-half*n*n); | |
} | |
return (1.0/n) as i32; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment