0

I have a simple script written in perl, and i keep getting this particular error when i try to run. The script is for generating some numbers for use in checking integer to floating point. This is the particular error i get.

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

From the error message am not able to figure out where my code is going wrong. By the way i need to use 64 bit numbers. How do i debug this issue?

Here is the code sample

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";
}
Gautam
  • 375
  • 2
  • 6
  • 23
  • I have done a mistake in the script where i convert number into its 2's complemt `$number = ($twoPwr32- $number +1) & 0xffffffff;` is shouldn't have added 1. – Gautam Feb 06 '15 at 04:52

1 Answers1

0

Try using use diagnostics to get a better error message and read perldoc bignum. The error and explanation is given there that usage of bignum internally converts the numbers into bignum and returns a reference. Since I have perl 5.14 documentation I have the link for documentation of perl 5.20 and I think the bug still exists. Refer to http://perldoc.perl.org/bignum.html

Update :

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.

Also refer to this question for the usage of 64 bit arithmetic in Perl.

Community
  • 1
  • 1
xtreak
  • 1,376
  • 18
  • 42
  • do i need to use bignum here? dose perl support 64 bit integer by default? Thanks for the pointers by the way – Gautam Feb 06 '15 at 04:45
  • I think the 64 bit support is lacking as I see from the error message I get with the usage of `use diagnostics`. You can run the program with `use diagnostics` for a description of the same. – xtreak Feb 06 '15 at 04:58