| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
type t = float |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
let is_nan (f : float) = f <> f |
| 19 |
let is_infinity f = f = infinity |
| 20 |
let is_neg_infinity f = f = neg_infinity |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
let to_string_raw fmt f = |
| 27 |
if is_nan f then "nan" |
| 28 |
else if is_infinity f then "infinity" |
| 29 |
else if is_neg_infinity f then "neg_infinity" |
| 30 |
else Printf.sprintf fmt f |
| 31 |
|
| 32 |
let to_hex_string = to_string_raw "%h" |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
let to_string = to_string_raw "%.17g" |
| 37 |
|
| 38 |
let of_string = float_of_string |
| 39 |
|
| 40 |
|
| 41 |
let compile f = |
| 42 |
Printf.sprintf "Float64.of_float (%s)" (to_hex_string f) |
| 43 |
|
| 44 |
let of_float f = f |
| 45 |
|
| 46 |
let to_float f = if is_nan f then nan else f |
| 47 |
|
| 48 |
let sign f = copysign 1. f < 0. |
| 49 |
|
| 50 |
let opp = ( ~-. ) |
| 51 |
let abs = abs_float |
| 52 |
|
| 53 |
type float_comparison = FEq | FLt | FGt | FNotComparable |
| 54 |
|
| 55 |
|
| 56 |
let eq (x : float) (y : float) = x = y |
| 57 |
[@@ocaml.inline always] |
| 58 |
|
| 59 |
let lt (x : float) (y : float) = x < y |
| 60 |
[@@ocaml.inline always] |
| 61 |
|
| 62 |
let le (x : float) (y : float) = x <= y |
| 63 |
[@@ocaml.inline always] |
| 64 |
|
| 65 |
|
| 66 |
let pervasives_compare (x : float) (y : float) = compare x y |
| 67 |
|
| 68 |
let compare (x : float) (y : float) = |
| 69 |
if x < y then FLt |
| 70 |
else |
| 71 |
( |
| 72 |
if x > y then FGt |
| 73 |
else |
| 74 |
( |
| 75 |
if x = y then FEq |
| 76 |
else FNotComparable |
| 77 |
) |
| 78 |
) |
| 79 |
[@@ocaml.inline always] |
| 80 |
|
| 81 |
type float_class = |
| 82 |
| PNormal | NNormal | PSubn | NSubn | PZero | NZero | PInf | NInf | NaN |
| 83 |
|
| 84 |
let classify x = |
| 85 |
match classify_float x with |
| 86 |
| FP_normal -> if 0. < x then PNormal else NNormal |
| 87 |
| FP_subnormal -> if 0. < x then PSubn else NSubn |
| 88 |
| FP_zero -> if 0. < 1. /. x then PZero else NZero |
| 89 |
| FP_infinite -> if 0. < x then PInf else NInf |
| 90 |
| FP_nan -> NaN |
| 91 |
[@@ocaml.inline always] |
| 92 |
|
| 93 |
let of_uint63 x = Uint63.to_float x |
| 94 |
[@@ocaml.inline always] |
| 95 |
|
| 96 |
let prec = 53 |
| 97 |
let normfr_mantissa f = |
| 98 |
let f = abs f in |
| 99 |
if f >= 0.5 && f < 1. then Uint63.of_float (ldexp f prec) |
| 100 |
else Uint63.zero |
| 101 |
[@@ocaml.inline always] |
| 102 |
|
| 103 |
let eshift = 2101 |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
let frshiftexp f = |
| 109 |
match classify_float f with |
| 110 |
| FP_zero | FP_infinite | FP_nan -> (f, Uint63.zero) |
| 111 |
| FP_normal | FP_subnormal -> |
| 112 |
let (m, e) = frexp f in |
| 113 |
m, Uint63.of_int (e + eshift) |
| 114 |
[@@ocaml.inline always] |
| 115 |
|
| 116 |
let ldshiftexp f e = ldexp f (Uint63.to_int_min e (2 * eshift) - eshift) |
| 117 |
[@@ocaml.inline always] |
| 118 |
|
| 119 |
external next_up : float -> float = "rocq_next_up_byte" "rocq_next_up" |
| 120 |
[@@unboxed] [@@noalloc] |
| 121 |
|
| 122 |
external next_down : float -> float = "rocq_next_down_byte" "rocq_next_down" |
| 123 |
[@@unboxed] [@@noalloc] |
| 124 |
|
| 125 |
let equal f1 f2 = |
| 126 |
if f1 = f2 then f1 <> 0. || sign f1 = sign f2 |
| 127 |
else is_nan f1 && is_nan f2 |
| 128 |
[@@ocaml.inline always] |
| 129 |
|
| 130 |
let hash = |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
Hashtbl.hash |
| 135 |
|
| 136 |
let total_compare f1 f2 = |
| 137 |
|
| 138 |
|
| 139 |
if f1 = 0. && f2 = 0. then pervasives_compare (1. /. f1) (1. /. f2) |
| 140 |
else pervasives_compare f1 f2 |
| 141 |
|
| 142 |
let is_float64 t = |
| 143 |
Obj.tag t = Obj.double_tag |
| 144 |
[@@ocaml.inline always] |