Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl strange behaviour on unpack of floating value

I have this snippet with a strange result (279.1... vs. 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 ones as well

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

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

What's wrong? Is that a bug in unpacking of non-native endian floating point values?

Note Perl is version 5.14.2 under Cygwin on a PC.

like image 325
Loic Avatar asked Sep 23 '13 22:09

Loic


2 Answers

This is a GCC problem.

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

like image 142
James Rushworth Avatar answered Nov 12 '22 07:11

James Rushworth


Definitely a bug in Perl's unpacking. It has difficulty in handling floats in the binary form xxxxyyFF at least in a 32-bit platform, where 80 <= yy <= BF. The packed result will become xxxxzzFF, where zz = yy + 40 (all in hexadecimal). And this is not a problem of endianness, as you could see here:

$ perl -e "print unpack('H8', pack ('f', unpack('f', pack('H8', '000088ff'))));"; 
0000c8ff
like image 1
kennytm Avatar answered Nov 12 '22 06:11

kennytm