in reply to Introspection into floats/NV

For curiosity and as i have a new license i asked ChatGPT:

#!/usr/bin/env perl + + + for m +y $val (qw(1 -1 2 -2 1/3 -1/3)) { my ($s, $e, $m) = double_to_bin_parts(eval $val); printf "%5s: sign=%s exp=%s mantissa=%s\n", $val, $s, $e, $m; } for my $expr (qw(1 -1 2 -2 1/3 -1/3)) { my $val = eval $expr; my ($s, $e, $m) = double_to_bin_parts($val); printf "%5s %.13a\n %s %s %s\n", $expr, $val, $s, $e, $m; } sub double_to_bin_parts { my $n = shift; my $bits = unpack "B64", pack "d>", $n; my $sign = substr $bits, 0, 1; my $exp = substr $bits, 1, 11; my $mantissa = substr $bits, 12, 52; return ($sign, $exp, $mantissa); } __END__ 1: sign=0 exp=01111111111 mantissa=00000000000000000000000000000000000 +00000000000000000 -1: sign=1 exp=01111111111 mantissa=0000000000000000000000000000000000 +000000000000000000 2: sign=0 exp=10000000000 mantissa=0000000000000000000000000000000000 +000000000000000000 -2: sign=1 exp=10000000000 mantissa=0000000000000000000000000000000000 +000000000000000000 1/3: sign=0 exp=01111111101 mantissa=010101010101010101010101010101010 +1010101010101010101 -1/3: sign=1 exp=01111111101 mantissa=01010101010101010101010101010101 +01010101010101010101 1 0x1.0000000000000p+0 0 01111111111 0000000000000000000000000000000000000000000000000000 -1 -0x1.0000000000000p+0 1 01111111111 0000000000000000000000000000000000000000000000000000 2 0x1.0000000000000p+1 0 10000000000 0000000000000000000000000000000000000000000000000000 -2 -0x1.0000000000000p+1 1 10000000000 0000000000000000000000000000000000000000000000000000 1/3 0x1.5555555555555p-2 0 01111111101 0101010101010101010101010101010101010101010101010101 -1/3 -0x1.5555555555555p-2 1 01111111101 0101010101010101010101010101010101010101010101010101

Replies are listed 'Best First'.
Re^2: Introspection into floats/NV
by LanX (Saint) on Jun 04, 2025 at 14:09 UTC

      I “discussed” your one liner and ikegamis sub and asked a question on pack/unpack similar to yours. Also impressive: A version of ikegamis sub with POD. The oracle liked the sub. A while ago i asked something about FFI::Platypus with Rust - which can be very tricky. The oracle provided a solution that i never would have found in the (good) docs.

        > I “discussed” your one liner and ikegamis sub

        By feeding our old code you gave it a reasonable headstart.

        It would have been interesting to know the solution without that.

        After googling I also found an old similar solution of our one and only ikegami at SO

        https://stackoverflow.com/questions/7669010/binary-representation-of-float-to-decimal-conversions-in-perl

        Most of my problems came from understanding the effect of little endian on the pack, till I discovered the ">" modifier.

        How can the AI know what hardware I'm using?

        It's would also be interesting to have more insights in the difference of "d" and "F" templates. I'm expecting the latter to be more portable.

        Supporting other NV formats from the compile options would be nice too.

        Note how I used B* not B64 in the unpack.

        Unfortunately I don't have other builds and hardware to test.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery