Created
January 29, 2019 11:23
-
-
Save stedolan/e417207a47e44163a0cbb0ab67bbaa4b to your computer and use it in GitHub Desktop.
This file contains 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
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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output with
%.7f
replaced with%f
: