### Re: OT: How much float precision needed for operation?

by trammell (Priest)
 on Sep 02, 2005 at 20:11 UTC ( #488773=note: print w/replies, xml ) Need Help??

```#!perl -l

# Looking at function F = pi ^ y
#
#             F = pi ^ y
#    error = dF = y * pi ^ (y-1) * dpi
# =>        dpi = error / ( y * x ^ (y-1) )
#
# So if we want to limit the error in F to 0.1:

use constant ERROR => 0.1;
use constant PI    => 4 * atan2(1,1);

warn "Check: pi is ", PI, "\n";

my @y = (1..9, map(10 * \$_, 1..9), map(100*\$_,1..10));

foreach my \$y (@y) {
printf "y=%d  dpi=%g\n", \$y, dpi(\$y);
}

sub dpi {
my \$y = \$_[0];
my \$denom = \$y * PI ** (\$y-1);
return ERROR / \$denom;
}

__END__
Check: pi is 3.14159265358979
y=1  dpi=0.1
y=2  dpi=0.0159155
y=3  dpi=0.00337737
y=4  dpi=0.000806288
y=5  dpi=0.00020532
y=6  dpi=5.44627e-05
y=7  dpi=1.48594e-05
y=8  dpi=4.13867e-06
y=9  dpi=1.171e-06
y=10  dpi=3.35468e-07
y=20  dpi=1.79111e-12
y=30  dpi=1.27507e-17
y=40  dpi=1.02116e-22
y=50  dpi=8.72341e-28
y=60  dpi=7.76258e-33
y=70  dpi=7.10495e-38
y=80  dpi=6.6385e-43
y=90  dpi=6.30114e-48
y=100  dpi=6.05568e-53
y=200  dpi=5.8364e-103
y=300  dpi=7.5001e-153
y=400  dpi=1.08428e-202
y=500  dpi=1.67203e-252
y=600  dpi=2.68581e-302
y=700  dpi=0
y=800  dpi=0
y=900  dpi=0
y=1000  dpi=0

Update: yes, for example if we're calculating pi^y for @y=5..15, and we introduce an error to pi of 1e-7:

```#!perl -l

use strict;
use warnings;
use constant PI    => 4 * atan2(1,1);
use constant DPI   => 1e-7;

my @y = (5..15);

foreach my \$y (@y) {
my \$f1 = F(PI,\$y);
my \$f2 = F(PI+DPI,\$y);
my \$delta = \$f2 - \$f1;
printf "y=%-2d  delta=%g\n", \$y, \$delta;
}

# F = pi ^ y
sub F {
my (\$pi, \$y) = @_;
return \$pi ** \$y,
}

__END__
y=5   delta=4.87045e-05
y=6   delta=0.000183612
y=7   delta=0.000672972
y=8   delta=0.00241623
y=9   delta=0.00853968
y=10  delta=0.0298091
y=11  delta=0.103013
y=12  delta=0.353045
y=13  delta=1.20155
y=14  delta=4.06515
y=15  delta=13.6833
So an error of 1e-7 in pi doesn't affect the output of F by more than 0.1 until we hit y=11.

Replies are listed 'Best First'.
Re^2: OT: How much float precision needed for operation?
by 5mi11er (Deacon) on Sep 02, 2005 at 21:02 UTC
So, I think what your data says is:
To ensure an error of no more than plus/minus 0.1 we'd need ~7 digits to the right of the decimal point for y=10 and ~53 digits for y=100
Is this correct?

-Scott

Update: trammel, thank you very much for your excellent examples above.

So, for others who want to extend this to other functions:

If we take the derivative of the function we're working with, multiplied by a delta (difference), and know what our tolerance for error is, we can solve for the maximum delta that will be within our tolerance for error.

In math symbols, the process is:

• given function F
• find the derative -> dF
• maxdelta = errortolerance/dF

Create A New User
Node Status?
node history
Node Type: note [id://488773]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2017-10-17 01:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My fridge is mostly full of:

Results (218 votes). Check out past polls.

Notices?