Perl 长数据类型

发布于 2024-10-13 22:39:49 字数 154 浏览 1 评论 0原文

我正在用 Perl 编写一个应用程序,它需要长数据类型而不是整数。我怎样才能做到这一点。例如;

my $num = sprintf('%ld', 27823221234);

print $num;

输出不是长整型,而是整数。

I am writing an app in Perl that requires long data type instead of integers. How can I achieve this. For example;

my $num = sprintf('%ld', 27823221234);

print $num;

The output is not a long, but an integer.

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

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

发布评论

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

评论(4

×眷恋的温暖 2024-10-20 22:39:50

您的选择是:

  • 使用为 64 位编译的 Perl
  • 使用 Math::Int64
  • 使用 < a href="http://search.cpan.org/perldoc?Math::BigInt" rel="noreferrer">Math::BigInt

更新:
啊,你也可以使用浮点数代替整数:

printf("%.0f", 2**50)

IIRC,在大多数当前架构上,浮点数可以精确地表示高达 2**54-1 的整数。

Your options are:

update:
ah, you can also use floats instead of integers:

printf("%.0f", 2**50)

IIRC, on most current architectures, floats can represent integers up to 2**54-1 precisely.

鲜血染红嫁衣 2024-10-20 22:39:50

在您的情况下 27823221234 实际上表示为 double,因此当您尝试输入 sprintf 时,您会收到 -1

my $x = 27823221234;

my $num = sprintf('%lf', $x);

print $num, "\n";

收益,

27823221234.000000

如果您想对大整数进行数学运算,请考虑使用 Math::Bigint 模块。

in your case 27823221234 is really represented as double, so when you try to feed to to sprintf you receive -1

my $x = 27823221234;

my $num = sprintf('%lf', $x);

print $num, "\n";

yields to

27823221234.000000

if you want to do math operations with large integers, consider using Math::Bigint module.

天邊彩虹 2024-10-20 22:39:50

下面是一些代码,说明了 Perl 的一些行为方式 - 源自您的示例:

use strict;
use warnings;

my $num = sprintf("%ld", 27823221234);
print "$num\n";

my $val = 27823221234;
my $str = sprintf("%ld", $val);
printf "%d = %ld = %f = %s\n", $val, $val, $val, $val;
printf "%d = %ld = %f = %s\n", $str, $str, $str, $str;

使用 64 位 Perl,结果是:

27823221234
27823221234 = 27823221234 = 27823221234.000000 = 27823221234
27823221234 = 27823221234 = 27823221234.000000 = 27823221234

如果您确实需要大数字(数百位数字),那么请查看支持它们的模块。例如:

Here is some code that illustrates some of how Perl behaves - derived from your example:

use strict;
use warnings;

my $num = sprintf("%ld", 27823221234);
print "$num\n";

my $val = 27823221234;
my $str = sprintf("%ld", $val);
printf "%d = %ld = %f = %s\n", $val, $val, $val, $val;
printf "%d = %ld = %f = %s\n", $str, $str, $str, $str;

With a 64-bit Perl, this yields:

27823221234
27823221234 = 27823221234 = 27823221234.000000 = 27823221234
27823221234 = 27823221234 = 27823221234.000000 = 27823221234

If you really need big number (hundreds of digits), then look into the modules that support them. For example:

永不分离 2024-10-20 22:39:50

你可能很困惑。 Perl 本身支持“长”大小的整数数学,但我不认为它的内部表示是你的问题所在。你期望你的输出是什么样的?

You are probably confused. Perl natively supports "long"-sized integer math, but I don't think its internal representation is where your problem is. What are you expecting your output to look like?

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