Sidharth Malhotra wrote: > I encountered a problem with floating point arithmetic that I'm afraid > may be causing greater calculation errors in my program. The program > I'm writing calculates numerical solutions for ordinary differential > equations (http://sid.cwru.edu/perl/euler.pl). What's happening is > that floating point storage is not rounding off properly and down the > line > 2.4 + 0.025 = 2.42499999999999 instead of 2.425. You can easily spot > the error in this short script: > > my $num = 1; > my $add = 0.025; > while ($num < 4) { > $num += $add; > print $count++ . " $num\n"; > } > The resulting error in my particular differential equation is 2/10^15 > which is fine exept the the error gets compounded over a few hundred > iterations. Does anyone have any suggestions for how to improve upon > this? I do understand that there are accuracy limitations with floating > pt. nums.
Numerical Computations is a very complicated and ugly obsession :-( I'm quite only a hacker ;-( But often, It's useful to avoid long iterations on sum. E.g. the upon script could be written as: my $num = 1; my $add = 0.025; while ($num < 4) { $num = $add * ($count+1); print $count++, " $num\n"; } Another way is to round while calculations if you exactly know how many digits your intermediate result have. E.g. in the above example it's clear that in every step $num has always the form \d\.\d{0,3}. So you can reduce the little calculation error by rounding: my $num = 1; my $add = 0.025; while ($num < 4) { $num += $add; # I add 0.0005 to $num -> rounding up $num = int ((1000 * $num) + 5) / 1000; print $count++, " $num\n"; } Well, to say to truth, I can't really help. But your adress (http://sid.cwru.edu/perl/euler.pl) brings a read time out as I tried. Best Wishes, Andrea -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]