Print a rational number in binary format

I have a number x such that 0 <= x < 1.0.
I would like to print x in binary format.

e.g. x=1/1024 prints “0.0000000001”
x=1/2 prints “0.1”

Am I right that the stdlib’s Printf “only” supports octal, decimal and hexadecimal?

I guess I am good for coding the conversion then.

You can get the binary representation of a float using Int64.bits_of_float which will give you the bit pattern you are looking for except for the scaling.

1 Like

As far as I know, yes.
Although it looks like the %b specifier for booleans is deprecated (with %B as the recommended alternative), possibly with the aim to reuse it for integers in binary format in a future release.