Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Floating Point Errors

by starbolin (Hermit)
on May 27, 2008 at 21:04 UTC ( [id://688739]=note: print w/replies, xml ) Need Help??


in reply to Floating Point Errors

That's a tall order. There exists many error sources when performing floating point numerical calculations. There have been whole books written on the subject. I would start by reading the references in the Wikipedia article

The most important thing is to use the right algorithm. Know the error bounds of your algorithm and know how to do a sensitivity analysis. If your code is generating infinitesimals, then you need to change your approach or use guard digits. Without guard digits any small difference calculations are more likely to be erroneous than to be correct.


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

Replies are listed 'Best First'.
Re^2: Floating Point Errors
by Anonymous Monk on May 28, 2008 at 13:56 UTC
    would adding like 00000001 to the end of my number (like: 32.41200000001) help?

      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}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://688739]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-24 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found