【发布时间】: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) & 0xffffffff;不应该添加 1。
标签: perl