I can't say as I don't know what operations you intend to perform on you 'number'. In general, the practice of adding an epsilon to floats just moves the problem around and does nothing but, perhaps, increase roundoff errors. Representational errors in floats are not evenly distributed . Take for example the following code:

print "Division by N\n"; for my $n (1..100) { if ( $n/100 == 0.01*$n ) { print "t" } else { print "n" } } Division by N ttttttttttttttttttttttttttttttttttntttttntttttntttttttttntttttttttttnn +tttttttttttnnttttttttttnnttttt
In an ideal world the code should always return true but for some values returns false due to an imprecise binary representation for $n.

Now if you are only doing an operation once the need to check the results it is acceptable to do the equality like this:

if (( x - y ) > epsilon ) then true

Also consider that 1e-8 is also not exactly representable in binary:

perl -e 'our $sum;for(1..1000000){$sum+=0.00000001};printf "%1.20f\n", + $sum' 0.00999999999994859168
This leads to the rule: Make all constants integer multiples of 2^n. i.e.
perl -e 'printf "%0.20f\n", 2/0x1000000' [11:08am] 0.00000011920928955078
This is especially important if using small divisors.

These methods falls apart if you are doing any kind of loop that allows the errors to accumulate; such as, Newtons Method, averages, Runge-Kutta, least-squares and etc. In these methods the error can grow to unanticipated magnitudes such to overwhelm a small, fixed, epsilon. Special methods are then needed that anticipate and correct for roundoff.


s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}

In reply to Re^3: Floating Point Errors by starbolin
in thread Floating Point Errors by Anonymous Monk

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.