Created
May 31, 2017 05:46
-
-
Save Manishearth/550c4f46c527596c5fb79a5adf49ac4b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#[inline(never)] | |
fn stylo(intpart: &[u32], fracpart: &[u32]) -> f32 { | |
let mut integral = 0.0f64; | |
let mut fractional = 0.0f64; | |
let mut factor = 0.1; | |
for i in intpart { | |
integral = integral * 10. + *i as f64; | |
} | |
for i in fracpart { | |
fractional += *i as f64 * factor; | |
factor *= 0.1; | |
} | |
1. *(integral + fractional) as f32 / 100. | |
} | |
#[inline(never)] | |
fn gecko(intpart: &[u32], fracpart: &[u32]) -> f32 { | |
let mut integral = 0; | |
let mut fractional = 0.0f64; | |
let mut divisor = 10.; | |
for i in intpart { | |
integral = integral * 10 + *i; | |
} | |
for i in fracpart { | |
fractional += *i as f64 / divisor; | |
divisor *= 10.; | |
} | |
(((integral as f64) + fractional) / 100.) as f32 | |
} | |
fn main(){ | |
let intpart = [4, 7]; | |
let fracpart = [9, 9, 5]; | |
println!("{:.10} {:.10}", stylo(&intpart, &fracpart) * 100., gecko(&intpart, &fracpart) * 100.); | |
println!("{:.10} {:.10}", 47.995f64 as f32 / 100., (47.995f64 / 100.) as f32); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment