Perl strange behavior when unpacking floating value

I have this snippet with a strange result (279.1 ... versus 279.6 ...):

$ perl -e "print unpack('f>', pack ('f>', 279.117156982422));" 279.617156982422 

While this one works

 $ perl -e "print unpack('f>', pack ('f>', 279.117256982422));" 279.117248535156 

And those and others

 $ perl -e "print unpack('f<', pack ('f<', 279.117156982422));" 279.11715698242 $ perl -e "print unpack('f', pack ('f', 279.117156982422));" 279.117156982422 

What happened? Is this an error when unpacking unsaturated floating point values?

Note. Perl is version 5.14.2 under Cygwin on PC.

+6
source share
2 answers

This is a GCC problem.

cpan -t Acme :: Study :: SREZIC transfers OK to my 32-bit systems, where the Perl binary is compiled with GCC 4.5.4 or 4.6.3 or 4.6.4 and does not transfer the systems on which the Perl binary is compiled with GCC 4.7.3 or 4.8.3

+1
source

Definitely a bug in unpacking Perl. It is difficult to process floats in binary form xxxxyyFF at least on a 32-bit platform, where 80 <= yy <= BF . The packed result will be xxxxzzFF , where zz = yy + 40 (all in hexadecimal format). And this is not a conjuncture problem, as you could see here:

 $ perl -e "print unpack('H8', pack ('f', unpack('f', pack('H8', '000088ff'))));"; 0000c8ff 
+1
source

Source: https://habr.com/ru/post/954457/


All Articles