Results 1 to 7 of 7
Thread: close to zero
 10232009, 02:09 AM #1Member
 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
 10232009, 02:39 AM #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.My Hobby Project: LegacyClone
 10232009, 03:01 AM #3Member
 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; }
 10232009, 03:03 AM #4Member
 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(n1) < 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; 10232009 at 04:15 PM.
 10232009, 08:40 AM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
 10262009, 04:41 PM #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.
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
 10262009, 05:30 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
Similar Threads

Close but help would be grateful
By reader34 in forum New To JavaReplies: 8Last Post: 12212008, 06:42 PM 
[SOLVED] Close Exe
By smartsubroto in forum New To JavaReplies: 11Last Post: 10142008, 09:04 AM 
Close Active Window (?)
By dcnorman07 in forum New To JavaReplies: 2Last Post: 10032008, 06:55 PM 
close a frame..
By tajinvillage in forum New To JavaReplies: 5Last Post: 04272008, 11:22 PM 
How to close a JFrame
By valery in forum New To JavaReplies: 1Last Post: 08062007, 06:33 PM
Bookmarks