C99-style hexadecimal floating-point constants in OCaml

178 views Asked by At

In OCaml, how can I parse C99-style floating-point constants (either as literals or inside strings) in hexadecimal, such as 0x1.b000000000000p4?

It seems that they are not valid literals:

# let c = 0x1.b000000000000p4;;
Characters 12-27:
  let c = 0x1.b000000000000p4;;
              ^^^^^^^^^^^^^^^
Error: Unbound record field b000000000000p4

And there seems to be no Scanf converter to parse them (e.g. a counterpart to C99's %a converter for printf).

1

There are 1 answers

3
Daniel Bünzli On BEST ANSWER

float_of_string should be able to parse them:

# float_of_string "0x1.199999999999Ap1";;
- : float = 2.2

However as Alain Frisch noted on OCaml's bug tracker the support actually depends on the underlying libc and won't currently work on the MSVC ports.