Int64.float_of_bits gives NaN for a negative integer?

According to my understanding, Int64.float_of_bits should give a valid representation for negative integers. However it gives NaN.

Rescript version: 10.1.4, currently trying on an M2 processor.

        Js.log("-2"->Int64.of_string->Int64.float_of_bits)

Is this a bug in Caml_int64.float_of_bits or in Rescript? Or am I misunderstanding how float_of_bits works?

2 Likes

Here is my guess: In OCaml the integers use two’s complement representation, so I’m assuming that Caml_int64.float_of_bits will be as well.

So 2 is

0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0010

and -2 in the two’s complement is

1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1110

And that interpreted as bits in IEEE 754

1 11111111111 1111111111111111111111111111111111111111111111111110
^ sign
  ^^exponent^
              ^^fraction^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

would be a representation of nan.

I would probably use BigInt instead of Int64 that has no support in JS.