Skip to content

Instantly share code, notes, and snippets.

@stedolan
Created January 29, 2019 11:23
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 stedolan/e417207a47e44163a0cbb0ab67bbaa4b to your computer and use it in GitHub Desktop.
Save stedolan/e417207a47e44163a0cbb0ab67bbaa4b to your computer and use it in GitHub Desktop.
let is_immediate_float bits =
let exp = (Int64.(to_int (shift_right_logical bits 52)) land 0x7FF) - 1023 in
let mant = Int64.logand bits 0xF_FFFF_FFFF_FFFFL in
exp >= -3 && exp <= 4 && Int64.logand mant 0xF_0000_0000_0000L = mant
let iter_immediate_floats f =
for sign = 0 to 1 do
for exp = -3 to 4 do
for mant = 0 to 15 do
let bits = Int64.(
shift_left (of_int sign) 63
|> logor (shift_left (of_int (exp + 1023)) 52)
|> logor (shift_left (of_int mant) 48)) in
f (Int64.float_of_bits bits)
done
done
done
let () =
iter_immediate_floats (fun f ->
assert (is_immediate_float (Int64.bits_of_float f));
if float_of_string (Printf.sprintf "%.7f" f) <> f then
Printf.printf "%.20f\n" f)
@stedolan
Copy link
Author

Output with %.7f replaced with %f:

0.13281250000000000000
0.14843750000000000000
0.16406250000000000000
0.17968750000000000000
0.19531250000000000000
0.21093750000000000000
0.22656250000000000000
0.24218750000000000000
-0.13281250000000000000
-0.14843750000000000000
-0.16406250000000000000
-0.17968750000000000000
-0.19531250000000000000
-0.21093750000000000000
-0.22656250000000000000
-0.24218750000000000000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment