Fix bug and make testing more reliable
This commit is contained in:
parent
8ff4748608
commit
53da38aa68
@ -5,5 +5,3 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
rayon = "1.10.0"
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
/// It is not actually necissary or even recommended to use this code.
|
/// It is not actually necissary or even recommended to use this code.
|
||||||
/// The compiler should always optimize division with constant denominators for you.
|
/// The compiler should always optimize division with constant denominators for you.
|
||||||
/// This code is just to help understand how the compilers optimization works.
|
/// This code is just to help understand how the compilers optimization works.
|
||||||
|
#[inline(always)]
|
||||||
pub fn div10(num: u32) -> (u32, u32) {
|
pub fn div10(num: u32) -> (u32, u32) {
|
||||||
// Calculate num / 10
|
// Calculate num / 10
|
||||||
// Note: 0.10 = 3435973837 / (1<<35)
|
// Note: 0.10 = 3435973837 / (1<<35)
|
||||||
@ -9,7 +10,7 @@ pub fn div10(num: u32) -> (u32, u32) {
|
|||||||
// Calculate quot * 10
|
// Calculate quot * 10
|
||||||
// Note: 10x = 2x + 4(2x)
|
// Note: 10x = 2x + 4(2x)
|
||||||
let quot2 = quot << 1;
|
let quot2 = quot << 1;
|
||||||
let quot8 = quot2 << 3;
|
let quot8 = quot2 << 2;
|
||||||
let quot10 = quot2 + quot8;
|
let quot10 = quot2 + quot8;
|
||||||
|
|
||||||
// Calculate the remainder
|
// Calculate the remainder
|
||||||
@ -22,14 +23,12 @@ pub fn div10(num: u32) -> (u32, u32) {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use rayon::prelude::*;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
/// This will take a REALLY long time
|
|
||||||
fn brute_check() {
|
fn brute_check() {
|
||||||
(0..=u32::MAX).into_par_iter().for_each(|i| {
|
for i in 0..=u32::MAX {
|
||||||
let ans = (i / 10, i % 10);
|
let ans = (i / 10, i % 10);
|
||||||
assert_eq!(ans, div10(i));
|
assert_eq!(ans, div10(i));
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user