【问题标题】:Can't use an undefined value as an ARRAY reference不能将未定义的值用作 ARRAY 引用
【发布时间】:2015-02-06 12:59:55
【问题描述】:

我有一个用 perl 编写的简单脚本,当我尝试运行时,我不断收到这个特殊错误。该脚本用于生成一些用于检查整数到浮点的数字。这是我得到的特定错误。

Can't use an undefined value as an ARRAY reference at /tools/oss/packages/i86pc-5.10/perl/5.8.8-32/lib/5.8.8/Math/BigInt/Calc.pm line 1180

从错误消息中我无法确定我的代码哪里出错了。顺便说一句,我需要使用 64 位数字。我该如何调试这个问题?

这是代码示例

use bignum;
use warnings;
use strict;

open(VCVT, ">CvtIntToFp") or die "couldn't open file to write:$!";

my $number;
my $sgn;

# left with 31 bits excluding the sign
# 23 bits of significand needed, all the result
# will be exact except where leading bit ignoring singn is >23
# take its 2's complement to get the negative number  and put it
# into the register
# 32 bit number 1 bit sign 31 left any number with leading 1 @position >23 (counting from 0) will be inexact when in floating point
# 30-24 bit positons can have a leading ones at at any position and result is an inexact

my $twoPwr32 = 0x100000000; #2**32

my @num=();

for(my $i=0; $i<100; $i++)
{
    $sgn = (rand()%2);
    my $tempLead = (rand()%7); # there are 7 bits from 24 to 30
    $number=$tempLead << 24;
    if($sgn)
    {$number = ($twoPwr32- $number +1) & 0xffffffff;
    }
    $number = sprintf("%x", $number);
    push(@num, $number);
}
my $item=0;
foreach $item (@num)
{
    print "$item\n";
    print VCVT "$item\n";
}

【问题讨论】:

  • 我在脚本中犯了一个错误,我将数字转换为它的 2 的补码 $number = ($twoPwr32- $number +1) &amp; 0xffffffff; 不应该添加 1。

标签: perl


【解决方案1】:

尝试使用use diagnostics 以获得更好的错误消息并阅读 perldoc bignum。那里给出了错误和解释,bignum 的使用在内部将数字转换为 bignum 并返回一个引用。因为我有 perl 5.14 文档,所以我有 perl 5.20 文档的链接,我认为这个错误仍然存​​在。参考http://perldoc.perl.org/bignum.html

更新:

Hexadecimal number > 0xffffffff non-portable at throw_stack.pl line 19 (#1)
    (W portable) The hexadecimal number you specified is larger than 2**32-1
    (4294967295) and therefore non-portable between systems.  See
    perlport for more on portability concerns.

另请参阅this 问题,了解 Perl 中 64 位算术的用法。

【讨论】:

  • 这里需要使用 bignum 吗?剂量 perl 默认支持 64 位整数?顺便谢谢指点
  • 我认为缺少 64 位支持,正如我从使用 use diagnostics 时收到的错误消息中看到的那样。您可以使用use diagnostics 运行该程序以获取相同的描述。
猜你喜欢
  • 2016-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多