Skip to content

Instantly share code, notes, and snippets.

@kali
Created July 19, 2020 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kali/3e515bae15715c3380ae925d2b531b9b to your computer and use it in GitHub Desktop.
Save kali/3e515bae15715c3380ae925d2b531b9b to your computer and use it in GitHub Desktop.
const TOINT: f32 = 1.0f32 / f32::EPSILON;
fn foo(x: f32) -> f32 {
let u = x.to_bits();
let e = u >> 23 & 0xff;
if e >= 0x7f + 23 {
return x;
}
let s = u >> 31;
let y = if s == 1 { x - TOINT + TOINT } else { x + TOINT - TOINT };
if y == 0.0 {
if s == 1 {
-0f32
} else {
0f32
}
} else {
y
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment