# close to zero

• 10-23-2009, 01:09 AM
nokicky
close to zero
Hey guys.
I've written a little program that finds the root of an equation using either the bisection or newtons method and it works great.
At the moment it is set up to do 25 iterations of each equation by which time an answer if often found to 6 decimal places.
I would like to stop the iterations if an answer if found before that.

So the question is, how do I compare a double to zero but to only 6 decimal places?

So that 0.0000001 == 0.000000 is true

Thanks
• 10-23-2009, 01:39 AM
mrmatt1111
with doubles and floats you should always use "<" and ">" and never use direct "=="

But if you insist, i would take a look at NumberFormat.
• 10-23-2009, 02:01 AM
nokicky
Thanks for the advise. Went for this option:

Code:

``` if((f.apply(x) < 0.0000001) && (f.apply(x) > -0.0000001)) {                 flag = false;             }```
• 10-23-2009, 02:03 AM
rdtindsm
Pretty standard to stop an iterative computation when a certain precision is met. May be interesting to examine a series of iterations to see the convergence, but the algorithm is usually stopped at some level of accuracy (which may be some small multiple) of machine accuracy).

Simply compare that iteration(n) - itneration(n-1) < required accuracy.
abs(thisX - lastX) <= somePrecision

be careful with this statement. It might allow extra iterations if each iteration alternates between (0 + epsilon) and (0 - epsilon). But not fatal, just doubles the accuracy.

A (very) quick look at NumberFormat suggests that it formats numbers to strings. Absolutely irrelevant to a numerical computation.

Edit: OP reposted while I was replying. He found a correct solution.
Edit2: It bothered me that the OP was comparing error range of the function evaluation to a precision value. While this may be what you want, the error in the root will be a polynomial mirroring in some way the original polynomial.

Most likely, you will have more accuracy than you are really looking for at the expense of extra iterations. You also won't really know what the accuracy of the final answer is.
• 10-23-2009, 07:40 AM
JosAH
Quote:

Originally Posted by nokicky
Thanks for the advise. Went for this option:

Code:

``` if((f.apply(x) < 0.0000001) && (f.apply(x) > -0.0000001)) {                 flag = false;             }```

Use the Math.abs( ... ) method to avoid double evaluatation of f.apply( ... )

Code:

`if (Math.abs(f.apply(x)) < 0.0000001) ...`
kind regards,

Jos
• 10-26-2009, 04:41 PM
xcallmejudasx
Would you mind sharing your code showing how you calculated the derivative? I attempted to write this program last year but other things came up and I never finished it.
• 10-26-2009, 05:30 PM
JosAH
Quote:

Originally Posted by xcallmejudasx
Would you mind sharing your code showing how you calculated the derivative? I attempted to write this program last year but other things came up and I never finished it.

This will do fine for very small values of h:

f'(x)== (f(x+h)-f(x-h))/2h

kind regards,

Jos