如何在 Perl 中解压双精度值?

发布于 2024-12-18 17:02:20 字数 940 浏览 2 评论 0原文

来自这个问题:

bytearray - Perl pack/unpack 和二进制字符串的长度 - Stack Overflow

我了解到,下面代码片段中的 @unparray = unpack("d "x5, $aa); 会在 unparray 中产生字符串项- 不是具有双精度数字(如我所料)。

是否有可能以某种方式从下面代码片段中的 $aa 字节字符串中获取双精度值数组?:

$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" '

提前非常感谢您的回答,
干杯!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海里的那抹蓝 2024-12-25 17:02:20

是什么让你认为它没有存储为双精度?

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

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
请爱~陌生人 2024-12-25 17:02:20

抱歉,您误解了霍布斯对您之前问题的回答。

$unparray[0] 双精度浮点值;但 length 与 C 的 sizeof 运算符不同,并且不会告诉您其参数的大小。相反,它将其参数转换为字符串,然后告诉您该字符串的长度。

例如,这个:

my $a = 3.0 / 1.5;
print length($a), "\n";

将打印这个:

1

因为它将 $a 设置为 2.0,它被字符串化为 2,长度为 1..

Sorry, but you've misunderstood hobbs' answer to your earlier question.

$unparray[0] is a double-precision floating-point value; but length is not like (say) C's sizeof operator, and doesn't tell you the size of its argument. Rather, it converts its argument to a string, and then tells you the length of that string.

For example, this:

my $a = 3.0 / 1.5;
print length($a), "\n";

will print this:

1

because it sets $a to 2.0, which gets stringified as 2, which has length 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文