in reply to How to convert negative and positive floating points to binary and vice versa
I'm far from an expert on internals, but IIRC, the "internal representation" of a number in Perl is as a binary 64-bit IEEE-754 float. You've only been looking at 8 bits (B8) of the 64. Note that the positive and negative numbers below differ in one single bit in their binary FP representations.
c:\@Work\Perl\monks>perl -wMstrict -le "my @ra = (0.000008, -0.000008); ;; for my $fpn (@ra) { my $pfpn = pack 'F', $fpn; printf qq{%8s: length when F-packed: %d bytes \n}, $fpn, length $pf +pn; } ;; for my $fpn (@ra) { my $pfpn = pack 'F', $fpn; printf qq{%8s: hex '%s' \n}, $fpn, unpack 'H*', $pfpn; } ;; for my $fpn (@ra) { my $pfpn = pack 'F', $fpn; printf qq{%8s: bits '%s' \n}, $fpn, unpack 'B*', $pfpn; } " 8e-006: length when F-packed: 8 bytes -8e-006: length when F-packed: 8 bytes 8e-006: hex '8dedb5a0f7c6e03e' -8e-006: hex '8dedb5a0f7c6e0be' 8e-006: bits '100011011110110110110101101000001111011111000110111000 +0000111110' -8e-006: bits '100011011110110110110101101000001111011111000110111000 +0010111110'
Update: See Wikipedia article IEEE floating point. (Update: Oh, and also see What Every Computer Scientist Should Know About Floating-Point Arithmetic and What Every Computer Scientist Should Also Know About Floating-Point Arithmetic — how could I forget?)
Update 2: Changed code example to include hex unpacking.
|
|---|