#!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.

In reply to Re: OT: How much float precision needed for operation? by trammell
in thread OT: How much float precision needed for operation? by 5mi11er

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.