diff options
| author | Pierre-Marie Pédrot | 2020-05-22 11:59:43 +0200 |
|---|---|---|
| committer | Pierre-Marie Pédrot | 2020-05-22 11:59:43 +0200 |
| commit | 7e09ee64b721baf0803c5fdb91c4687fded112cb (patch) | |
| tree | 9f3f691be9b0500e846fee282183d5e0975bec8c /kernel/float64.ml | |
| parent | 90389df4d03a6a6232e0372ff3efee720f85d284 (diff) | |
| parent | 04e22abe4378e29671def7b4d9c7e509c58ef6b6 (diff) | |
Merge PR #11986: [primitive floats] Add low level printing
Ack-by: SkySkimmer
Reviewed-by: ppedrot
Diffstat (limited to 'kernel/float64.ml')
| -rw-r--r-- | kernel/float64.ml | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/kernel/float64.ml b/kernel/float64.ml index 53fc13b04b..76005a3dc6 100644 --- a/kernel/float64.ml +++ b/kernel/float64.ml @@ -19,29 +19,27 @@ let is_nan (f : float) = f <> f let is_infinity f = f = infinity let is_neg_infinity f = f = neg_infinity -(* Printing a binary64 float in 17 decimal places and parsing it again - will yield the same float. We assume [to_string_raw] is not given a - [nan] or an infinity as input. *) -let to_string_raw f = Printf.sprintf "%.17g" f - (* OCaml gives a sign to nan values which should not be displayed as all NaNs are considered equal here. OCaml prints infinities as "inf" (resp. "-inf") but we want "infinity" (resp. "neg_infinity"). *) -let to_string f = +let to_string_raw fmt f = if is_nan f then "nan" else if is_infinity f then "infinity" else if is_neg_infinity f then "neg_infinity" - else to_string_raw f + else Printf.sprintf fmt f + +let to_hex_string = to_string_raw "%h" + +(* Printing a binary64 float in 17 decimal places and parsing it again + will yield the same float. *) +let to_string = to_string_raw "%.17g" let of_string = float_of_string (* Compiles a float to OCaml code *) let compile f = - let s = - if is_nan f then "nan" else if is_neg_infinity f then "neg_infinity" - else Printf.sprintf "%h" f in - Printf.sprintf "Float64.of_float (%s)" s + Printf.sprintf "Float64.of_float (%s)" (to_hex_string f) let of_float f = f |
