on 10/14/03 9:18 AM, Dan Sugalski at [EMAIL PROTECTED] wrote:

> You'd also be very ill-advised to do much financial math with floats--as
> you can see it takes very few operations to see penny-level errors.
> Multiply that by a couple of hundred thousand (or million) calculations
> for your average bank and, well, that's a lot of potential cash. I'd also
> *really* prefer you not do payroll calculations (at least not on *my*
> paycheck :) with floating point math. Fixed-point math (generally with 4
> decimal places and an implied decimal point on a 32 or 64 bit integer) is
> more appropriate in those cases.

That's exactly what's happening in our case (Bill Stephenson and I).

Seems that the mention of how many bits the current build of perl has (the
system compiler), that if it changes in the future, this will cause havoc
with the script.  So I would think a regex (albeit a slow version) would
solve this problem entirely since we are only concerned with a 2 digit
round.  Is this correct?

I also have concerns about the negative side of things.  Is this rounding
correctly on negatives?

Shawn McKinley

====================================
<code language="Perl">
#!perl -w

use strict;
use Integer;
printf qq~\n%-7s%-20s%-12s%-12s%-12s\n~,('Test #','Raw','Round');
print q~------ ------------------- ----------- ~,$/;
for my $t (1..25) {
  my $q=sprintf("%.5f",rand(1)*1000-rand(1)*1000);
  my $p=sprintf("%.5f",rand(1)*1000-rand(1)*1000);
  my $r=sprintf q~%12.2f~,&round($q,$p);
  printf qq~%-6s%20.12f~.$r.$/,$t,$q*$p;
}
print q~------ ------------------- ----------- ~,$/;
exit;

sub round {
  $_=sprintf("%015i",int((shift)*(shift)*1000));
  /^\-?(\d*)(\d\d)(\d)$/;
  my($whole,$fraction,$rounder)=($1,$2,$3);
  $fraction++ if($rounder 4);
  if($fraction 99) { $whole++; $fraction='00'; }
  return((/^\-/?'-':'').$whole.'.'.$fraction)
}
</code>

====================================



Reply via email to