N
niall.macpherson
I am using Win32::OLE to output data to an Excel Spreadsheet.
However I am finding that all integers between 2e31 and 2e32 are
displayed incorrectly. I get a negative number (can't remember whether
it is 1s complement or 2s complement
)
Printing the data using Data:
umper results in the correct values
being shown.
The problem disappears when the value reaches 2e32.
Sample code
##---------------------------------------------------------------------
use strict;
use warnings;
use Win32::OLE;
use Data:
umper;
##---------------------------------------------------------------------
sub ConnectToExcel
{
my $excel_conn;
eval
{
## check if already running
$excel_conn = Win32::OLE->GetActiveObject('Excel.Application')
};
die "Excel not installed" if $@;
unless (defined $excel_conn)
{
## if not , run it
## TODO Check what the $_[0]->Quit is about
$excel_conn = Win32::OLE->new('Excel.Application', sub
{$_[0]->Quit;})
or die "Failed to start Excel";
}
return($excel_conn);
}
##---------------------------------------------------------------------
my $excel_conn = ConnectToExcel();
my $work_book = $excel_conn->Workbooks->Add;
my $work_sheet = $work_book->Worksheets(1);
my $data = 1;
for(my $lcount = 29; $lcount <= 33; $lcount++)
{
my $data = (2 ** $lcount);
print Dumper "Adding $data ";
$work_sheet->Cells($lcount - 28, 1)->{Value} = $lcount;
$work_sheet->Cells($lcount - 28, 2)->{Value} = $data;
$work_sheet->Cells($lcount - 28, 3)->{Value} = $data + 1;
$work_sheet->Cells($lcount - 28, 4)->{Value} = $data - 1;
}
exit(0);
#------------------------------------------------------------------------------------------------
Output from Data:
umper
C:\develop\NiallPerlScripts>excel_bigint.pl
$VAR1 = 'Adding 536870912 ';
$VAR1 = 'Adding 1073741824 ';
$VAR1 = 'Adding 2147483648 ';
$VAR1 = 'Adding 4294967296 ';
$VAR1 = 'Adding 8589934592 ';
Values from the excel spreadsheet
29 536870912 536870913 536870911
30 1073741824 1073741825 1073741823
31 -2147483648 -2147483647 2147483647
32 4294967296 4294967297 4294967295
33 8589934592 8589934593 8589934591
Note the values for (2**31) and (2**31)+1 are incorrect. However
(2**31)-1 and (2**32) are displayed correctly.
Am I missing something I need to use here ? (Math::Bigint possibly - I
have seen it referred to before but never used it), or is it a bug in
the Win32::OLE module ?
Thanks
However I am finding that all integers between 2e31 and 2e32 are
displayed incorrectly. I get a negative number (can't remember whether
it is 1s complement or 2s complement
Printing the data using Data:
being shown.
The problem disappears when the value reaches 2e32.
Sample code
##---------------------------------------------------------------------
use strict;
use warnings;
use Win32::OLE;
use Data:
##---------------------------------------------------------------------
sub ConnectToExcel
{
my $excel_conn;
eval
{
## check if already running
$excel_conn = Win32::OLE->GetActiveObject('Excel.Application')
};
die "Excel not installed" if $@;
unless (defined $excel_conn)
{
## if not , run it
## TODO Check what the $_[0]->Quit is about
$excel_conn = Win32::OLE->new('Excel.Application', sub
{$_[0]->Quit;})
or die "Failed to start Excel";
}
return($excel_conn);
}
##---------------------------------------------------------------------
my $excel_conn = ConnectToExcel();
my $work_book = $excel_conn->Workbooks->Add;
my $work_sheet = $work_book->Worksheets(1);
my $data = 1;
for(my $lcount = 29; $lcount <= 33; $lcount++)
{
my $data = (2 ** $lcount);
print Dumper "Adding $data ";
$work_sheet->Cells($lcount - 28, 1)->{Value} = $lcount;
$work_sheet->Cells($lcount - 28, 2)->{Value} = $data;
$work_sheet->Cells($lcount - 28, 3)->{Value} = $data + 1;
$work_sheet->Cells($lcount - 28, 4)->{Value} = $data - 1;
}
exit(0);
#------------------------------------------------------------------------------------------------
Output from Data:
C:\develop\NiallPerlScripts>excel_bigint.pl
$VAR1 = 'Adding 536870912 ';
$VAR1 = 'Adding 1073741824 ';
$VAR1 = 'Adding 2147483648 ';
$VAR1 = 'Adding 4294967296 ';
$VAR1 = 'Adding 8589934592 ';
Values from the excel spreadsheet
29 536870912 536870913 536870911
30 1073741824 1073741825 1073741823
31 -2147483648 -2147483647 2147483647
32 4294967296 4294967297 4294967295
33 8589934592 8589934593 8589934591
Note the values for (2**31) and (2**31)+1 are incorrect. However
(2**31)-1 and (2**32) are displayed correctly.
Am I missing something I need to use here ? (Math::Bigint possibly - I
have seen it referred to before but never used it), or is it a bug in
the Win32::OLE module ?
Thanks