I don't know how many people here remember the thread on "weird math"
but here's the question that I posed awhile back...
I've got a script that takes numbers similar to those below and then
rounds
them and adds them together using code similar to what is below.
#
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 averag
> 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.
Math::BigFloat, perhaps?
they do. They just don't quite do what people think they do, which is
where the problems creep in.
Dan
> -Original Message-
> From: Dan Sugalski [mailto:[EMAIL PROTECTED]
> Sent: Monday, October 13, 2003 2:56 PM
> To: Bill Stephenson
> Cc: mac osx l
m: Dan Sugalski [mailto:[EMAIL PROTECTED]
Sent: Monday, October 13, 2003 2:56 PM
To: Bill Stephenson
Cc: mac osx list
Subject: Re: Weird math...
On Mon, 13 Oct 2003, Bill Stephenson wrote:
> I've got a script that takes numbers similar to those below and then
rounds
> them and adds them t
# An example that tells it all:
# 1 + 1/20 = 1.05= 1.00 0011 : bit52 = 0, bit53 = 1
# 1 + 1/40 = 1.025 = 1.000 0011: bit52 = 0, bit53 = 0
# 1 + 1/80 = 1.0125 = 1. 0011 : bit52 = 1, bit53 = 0
# 1 + 1/160 = 1.00625 = 1.0 0011 : bit52 = 1, bit53 = 1
# where
# 1.0
On Mon, 13 Oct 2003, Vic Norton wrote:
> You are wrong about the 64 bit part, Edward, no matter what the Perl
> books say.
Never tell an Apple employee they are wrong, especially on a statement of
machine or software capability. Unless you are sure. And even then,
double check two or three time
You are wrong about the 64 bit part, Edward, no matter what the Perl books say.
The precision, eps, of a machine is the smallest positive number such that
1 + eps > 1 in machine arithmetic. For example, if
1.01 > 1 but 1.001 = 1 (binary),
then the precision of the machine is 0.
on 10/13/03 3:07 PM, John Delacour at [EMAIL PROTECTED] wrote:
> Don't ask me why but this does solve your problem (which happens also
> in 5.8.0):
>
> @ls = qw(1.655 1.755) ;
> for (@ls) {
> $_ += 0.01;
> printf "%.2f$/" , $_;
> }
>
>
> JD
Thanks JD! That seems to work pretty good
Yes, Vic is correct (except Perl uses double precision, which is 64
bits). I wrote a C program that prints the floating point to floating
point values to 20 decimal places, and include the numbers 1 bit less
and 1 bit more than those numbers:
1.65480460 1.65502665 1.655
Here is a little Perl routine that shows exactly what is going on.
#!/usr/bin/perl -w
$bigtwo = 2**32;
while ($a = ) {
$b = (sprintf "%.0f", $a * $bigtwo)/$bigtwo;
$up_or_down = $b >= $a ? "round up" : "round down";
print "$a $up_or_down\n";
}
__END__
1.655
Here's a short rounding routine that uses sprintf and does produce
correct results.
#!/usr/bin/perl -w
while () { print round($_,2), "\n" }
sub round {
my ($number, $places) = @_;
my $tenpower = 10**$places;
return (sprintf "%.0f", $number * $tenpower)/$tenpower;
}
__END
I've got a script that takes numbers similar to those below and then rounds
them and adds them together using code similar to what is below.
#!/usr/bin/perl -w
use strict;
my $item_1 = "1.655";
my $item_2 = "1.755";
my $rnd_1 = (sprintf qq~%.2f~, $item_1);
my $rnd_2 =
At 03:24 PM 10/13/2003, Bill Stephenson wrote:
on 10/13/03 1:56 PM, Dan Sugalski at [EMAIL PROTECTED] wrote:
> Welcome to the wonderful world of inexact representations.
Thank you!! I'll read up on this today, and hopefully the workarounds will
suffice for my customers.
Have you seen this pape
Don't ask me why but this does solve your problem (which happens also
in 5.8.0):
@ls = qw(1.655 1.755) ;
for (@ls) {
$_ += 0.01;
printf "%.2f$/" , $_;
}
JD
At 1:49 pm -0500 13/10/03, Bill Stephenson wrote:
I've got a script that takes numbers similar to those below a
In a message dated Mon, 13 Oct 2003, Bill Stephenson writes:
> #!/usr/bin/perl -w
> use strict;
> my $item_1 = "1.655";
> my $item_2 = "1.755";
> my $rnd_1 = (sprintf qq~%.2f~, $item_1);
> print "$rnd_1, $rnd_2";
>
> The code above prints this result (system 10.1.5, perl 5.6
Granted this a bit off-topic to Perl & Mac OS X, but it is just too fun a question to
pass up during lunch!
Below is some code on you might resolve your problem...or at least is fun to run
(okay, I think it is fun to run). In the end, it may be too convulated an example of
an alternative means
on 10/13/03 1:56 PM, Dan Sugalski at [EMAIL PROTECTED] wrote:
> Welcome to the wonderful world of inexact representations.
Thank you!! I'll read up on this today, and hopefully the workarounds will
suffice for my customers.
--
Bill Stephenson
www.PerlHelp.com
1-417-546-5593
On Mon, 13 Oct 2003, Bill Stephenson wrote:
> I've got a script that takes numbers similar to those below and then rounds
> them and adds them together using code similar to what is below.
[snip]
> The code above prints this result (system 10.1.5, perl 5.6.0):
>
> 1.66, 1.75
>
> But shouldn't
I've got a script that takes numbers similar to those below and then rounds
them and adds them together using code similar to what is below.
#!/usr/bin/perl -w
use strict;
my $item_1 = "1.655";
my $item_2 = "1.755";
my $rnd_1 = (sprintf qq~%.2f~, $item_1);
my $rnd_2 =
20 matches
Mail list logo