Last active
July 9, 2023 16:46
-
-
Save cppio/3f5a4ef18d8268ff3fe8530fac7e357d 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
| //type F = f32; | |
| //type U = u32; | |
| type F = f64; | |
| type U = u64; | |
| pub fn sample_a(i: U) -> F { | |
| let d = 2.0 / F::EPSILON; | |
| (i % d as U) as F / d | |
| } | |
| pub fn sample_b(i: U) -> F { | |
| let b = F::MANTISSA_DIGITS - 1; | |
| let i = i % (2 << b); | |
| F::from_bits(match i.checked_ilog2() { | |
| None => 0, | |
| Some(e) => U::from(F::MAX_EXP as u32 - b - 2 + e) << b | (i ^ 1 << e) << (b - e), | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment