Home > Archive > PERL Beginners > April 2004 > Floating Point Number - Wierd
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Floating Point Number - Wierd
|
|
| Paul D. Kraus 2004-04-22, 2:34 pm |
| I am running a simple script that is reading through a text file and adding up
floating point numbers from each line.
At the 2165 line the number goes wrong. All the numbers are money \d+.\d\d
all numbers in the hundreds to thousands.
I have removed lines thinking maybe something was screwy with the data. If i
erase the line its the next on.
If I am manually insert a line with a number it becomes that one.
Code below.... (I know its ugly but its one of those 1 time scripts that is
built as you go to massage the data) The only part that is relevent to this
issue is the total counter. $total.
here is a sample of the data....
585-00-00 Other Purchases SUPE01 1031776
76.38
585-00-00 Other Purchases SURE00 00062967
228.00
585-00-00 Other Purchases SURE00 00063067
212.64
585-00-00 Other Purchases SWED00 066804
171.00
585-00-00 Other Purchases SWED00 067034
219.75
585-00-00 Other Purchases SWED00 067140
143.00
585-00-00 Other Purchases SWED00 067187
178.50
585-00-00 Other Purchases SWED00 067297
221.50
585-00-00 Other Purchases SWED00 067408
23.00
585-00-00 Other Purchases SWED00 067411
92.00
585-00-00 Other Purchases SWED00 067564
106.00
585-00-00 Other Purchases SWED00 067677
111.00
585-00-00 Other Purchases SWED00 067830
373.50
585-00-00 Other Purchases TECH02 10406
680.16
585-00-00 Other Purchases TECH02 10414
565.04
585-00-00 Other Purchases TECH02 10479
687.96
585-00-00 Other Purchases TECH02 10480
679.08
Any ideas?
Paul
Code
--------
#!/usr/bin/perl
use strict;
use warnings;
my %vendors;
my $total;
my $credits;
my $linecount;
while ( <> ) {
chomp;
next unless ( /^\d\d\d-\d\d-\d\d/ ) ;
if ( /([\w\&]{4}\d\d)\s+/){
my $vendor = $1;
my @record = split /\s+/, $';
$record[1]=~ s/,//g;
if ( length( $_) == 121 ) {
$credits += $record[1];
$record[1] *= -1;
}
$vendors{$vendor} += $record[1];
$total += $record[1];
$linecount++;
print "$linecount:$total\n";
if ( $total =~ /\.\d\d9/ ){
print "$_\n";
print "$total\n";
die;
}
} else {
}
}
open ( LONG, "<long.txt" );
my %long;
my $longtotal;
while ( <LONG> ) {
chomp;
if ( /^\s{66}Vendor/ ) {
my $record = $';
my @record = split /\s+/, $record;
$_ =~ s/^\s+|\s+$//g foreach @record;
$record[4] =~ s/,//g;
$long{ $record[1] } = $record[4];
$longtotal += $record[4];
}
}
foreach my $vendor( sort keys %long ){
printf ( "%-10s %13f\n", $vendor, $long{$vendor} );
}
# die;
foreach my $vendor ( sort keys %vendors ) {
printf ( "%-10s %13f\n", $vendor, $vendors{$vendor} );
}
print "shorttotal:" . ($total - 3014.73) ."\n";
print "shorttotal:$total\n";
print "credits:" . ($credits + 1636519.35 + 3014.73) . "\n";
print "longtotal:$longtotal\n";
| |
| Paul D. Kraus 2004-04-22, 2:34 pm |
|
On Thursday 22 April 2004 01:47 pm, Paul D. Kraus wrote:
> I am running a simple script that is reading through a text file and adding
> up floating point numbers from each line.
> At the 2165 line the number goes wrong. All the numbers are money \d+.\d\d
> all numbers in the hundreds to thousands.
I fixed it by when the count got to 2164 i dumped the amount int a subtotal
variable. then reset total to 0. Then at the end I add subtotal back to the
new total.
Very odd...
Paul
| |
| Wiggins D Anconia 2004-04-22, 4:33 pm |
| > I am running a simple script that is reading through a text file and
adding up
> floating point numbers from each line.
> At the 2165 line the number goes wrong. All the numbers are money \d+.\d\d
> all numbers in the hundreds to thousands.
>
> I have removed lines thinking maybe something was screwy with the
data. If i
> erase the line its the next on.
> If I am manually insert a line with a number it becomes that one.
>
>
> Code below.... (I know its ugly but its one of those 1 time scripts
that is
> built as you go to massage the data) The only part that is relevent to
this
> issue is the total counter. $total.
>
Problem could be doing the floating point math. You might want to
consider translating your money values into pennies and using only
integer math, then going back to dollars at the end.
perldoc perlnumber
For more...
http://danconia.org
|
|
|
|
|