Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I htink this looks horrible, but is not :-)
You noticed the first time the error gets less than zero! If you calculate the error, you'll see it's around 10E-15 at this point (at last on my pc).
#!/usr/bin/perl my $x=0.0; my $y=0; my $e_min=1.0; my $e_max=0.0; my $n=0; my $e; for ( 1 .. 999 ) { $x += 0.05; $y += 5; $e = abs($x - $y / 100); $n++ if $e == 0.0; $e_min = $e if $e > 0.0 && $e < $e_min; $e_max = $e if $e > $e_max; printf "%4d x = %.30f e = %E\n", $_, $x, $e; } printf "min %E max %E exact %d\n", $e_min, $e_max, $n; result 998 x = 49.899999999999302247033483581617 e = 6.963319E-13 999 x = 49.949999999999299404862540541217 e = 7.034373E-13 min 2.775558E-17 max 7.034373E-13 exact 16
You'll find the lines arround 190 interesting:
 189 x = 9.449999999999999289457264239900 e = 0.000000E+00
 190 x = 9.500000000000000000000000000000 e = 0.000000E+00
 191 x = 9.550000000000000710542735760100 e = 0.000000E+00

In reply to Re: adding numbers and floating point errors by Anonymous Monk
in thread adding numbers and floating point errors by smeenz

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found