为什么我的 Perl bigint 有小数位?

发布于 2024-07-21 00:32:02 字数 324 浏览 6 评论 0原文

如果我在 substr 中使用大整数:

use BigInt;
$acct_hash = substr(('99999999999912345' + $data[1]),0,15);

为什么结果仍然是 9.9999999999912

我期待999999999999912。 有类似这样的东西吗?

$data[1] = substr(to_char('999999999999991234'),0,15); 

Perl 中

If I do use a big integer in substr:

use BigInt;
$acct_hash = substr(('99999999999912345' + $data[1]),0,15);

why is the result still 9.9999999999912?

I was expecting 999999999999912. Is there something like:

$data[1] = substr(to_char('999999999999991234'),0,15); 

in Perl?

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

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

发布评论

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

评论(2

凉风有信 2024-07-28 00:32:02

要获取 $a$b 之和的前 15 位数字,请执行以下操作:

use bigint;
my $a = "99999999999912345";  # works with or without quotes
my $b = "111";                # works with or without quotes
print substr(0 + $a + $b, 0, 15), "\n";

您的代码未按预期工作的原因是 Perl 执行了浮动如果 $a$b 都是字符串,则 $a + $b 的点加法,即使 使用 bigint已生效。 示例:

use bigint;
print     1234567890123456789  +  2 , "\n";  #: 1234567890123456791
print    "1234567890123456789" +  2 , "\n";  #: 1234567890123456791
print     1234567890123456789  + "2", "\n";  #: 1234567890123456791
print    "1234567890123456789" + "2", "\n";  #: 1.23456789012346e+18
print 0 + 1234567890123456789  + "2", "\n";  #: 1234567890123456791

此行为是 Perl bigint 模块中的一个怪癖。 您可以通过在前面添加 0 + (如上所示)来解决此问题,从而强制 bigint 加法而不是浮点加法。 另一种解决方法可以是 Math::BigInt->new($a) + $b 而不是 0 + $a + $b

To get the first 15 digits of the sum of $a and $b, do this:

use bigint;
my $a = "99999999999912345";  # works with or without quotes
my $b = "111";                # works with or without quotes
print substr(0 + $a + $b, 0, 15), "\n";

The reason why your code didn't work as expected is that Perl does a floating point addition for $a + $b if both $a and $b are strings, even if use bigint is in effect. Example:

use bigint;
print     1234567890123456789  +  2 , "\n";  #: 1234567890123456791
print    "1234567890123456789" +  2 , "\n";  #: 1234567890123456791
print     1234567890123456789  + "2", "\n";  #: 1234567890123456791
print    "1234567890123456789" + "2", "\n";  #: 1.23456789012346e+18
print 0 + 1234567890123456789  + "2", "\n";  #: 1234567890123456791

This behavior is a quirk in the Perl bigint module. You can work it around by prepending a 0 + (as shown above), thus forcing bigint addition instead of floating point addition. Another workaround can be Math::BigInt->new($a) + $b instead of 0 + $a + $b.

走过海棠暮 2024-07-28 00:32:02

我认为你遇到的是字符串解释的问题。 试试这个代码:

use bigint;
print(99999999999912345 + 99999999999912345, "\n");

并将其与非常相似的代码进行比较:

use bigint;
print('99999999999912345' + '99999999999912345', "\n");

在数字周围使用单引号会将它们转换为字符串,并且似乎可以绕过 bigint

I think what you're running into is a problem with string interpretation. Try this code:

use bigint;
print(99999999999912345 + 99999999999912345, "\n");

And compare it to the very similar:

use bigint;
print('99999999999912345' + '99999999999912345', "\n");

Using single quotes around your numbers is turning them into strings, and seems to get around bigint.

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