Skip to content

Instantly share code, notes, and snippets.

@cppio
Last active July 9, 2023 16:46
Show Gist options
  • Select an option

  • Save cppio/3f5a4ef18d8268ff3fe8530fac7e357d to your computer and use it in GitHub Desktop.

Select an option

Save cppio/3f5a4ef18d8268ff3fe8530fac7e357d to your computer and use it in GitHub Desktop.
//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