# Thread: close to zero

1. Member
Join Date
Oct 2009
Posts
2
Rep Power
0

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

2. with doubles and floats you should always use "<" and ">" and never use direct "=="

But if you insist, i would take a look at NumberFormat.

3. Member
Join Date
Oct 2009
Posts
2
Rep Power
0
Thanks for the advise. Went for this option:

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

4. Member
Join Date
Feb 2009
Posts
92
Rep Power
0
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.
Last edited by rdtindsm; 10-23-2009 at 03:15 PM.

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

Java 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( ... )

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

Jos

6. 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.

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•