Since Perl v5.22 added hexadecimal floating-point numbers, I investigated how floating point numbers are actually represented. These are specified in IEEE 754 and are something you’ve probably taken for granted. I can use Inline::C to play with these.

Perl stores floating point numbers as “doubles”, a particular sort of floating-point number. More correctly, it’s a double-precision floating-point number.

For a 64-bit floating-point number, I don’t get 64 bits to represent the number. I get 52 bits (not quite true, but true enough for now). The other 12 bits represent the sign and the exponent:

Bits Total bits Range Use 63 1 0 to 1 Sign bit 62-52 11 0 to 2047 Exponent 51-0 52 0 to 251 Mantissa

And, I can’t (well, not me personally, but maybe someone else can) look at these bits and tell you want the number is. Before I show the rules to turn these bits into a number, I want to see the actual bits.

In my Intermediate Perl class, I briefly introduce the Inline::C module. This bit of magic turns C code, which I put right in my Perl source, into Perl functions. It does various type conversions too. For example, Perl stores its values in variables with lots of extra bookkeeping. I can pass a scalar variable to a C function without worrying about the Perl types: