How do I unpack a double-precision value in Perl?

From this question:

bytearray - Perl pack/unpack and length of binary string - Stack Overflow

I've learned that @unparray = unpack("d "x5, $aa); in the snippet below results with string items in the unparray - not with double precision numbers (as I expected).

Is it possible to somehow obtain an array of double-precision values from the $aa bytestring in the snippet below?:

$a = pack("d",255);
print length($a)."\n";
# prints 8

$aa = pack("ddddd", 255,123,0,45,123);
print length($aa)."\n";
# prints 40

@unparray = unpack("d "x5, $aa);
print scalar(@unparray)."\n";
# prints 5

print length($unparray[0])."\n" 
# prints 3

printf "%d\n", $unparray[0] '
# prints 255

# one liner:
# perl -e '$a = pack("d",255); print length($a)."\n"; $aa = pack("ddddd", 255,123,0,45,123); print length($aa)."\n"; @unparray = unpack("d "x5, $aa); print scalar(@unparray)."\n"; print length($unparray[0])."\n" '

Many thanks in advance for any answers, Cheers!

Answers


What makes you think it's not stored as a double?

use feature qw( say );

use Config      qw( %Config );
use Devel::Peek qw( Dump );

my @a = unpack "d5", pack "d5", 255,123,0,45,123;

say 0+@a;             # 5
Dump $a[0];           # NOK (floating point format)
say $Config{nvsize};  # 8 byte floats on this build

Need Your Help

Trying to get Cucumber working with OmniAuth

ruby-on-rails cucumber omniauth

I have been pulling my hair out because of this.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.