im trying to program a little java program, that finds null points in a given function f(x).
This is my idea:
I have X1 and X2 (User input) to define an area where to search for the null point. For example -5 and 5.
private static double Naehern(double X, double X2) {
double Y = -1;
double Step = 0.01;
while(Y < -0.00001 || Y > 0.00001) {
X = X + Step;
Y = f(X);
if(X >= X2) {
break;
}
}
return X;
}
static double f(double x) {
return x * x;
}
I am trying to find a Point X, between X1 and X2, where our null point is with an accuracy of about 5. So, in my while() i am starting at X1 and keep checking for X1 while adding +0,01 to it. At some point it should find a null point.
Problems in my current idea:
When starting with -5 for X1 and 5 for X2, the following happens:
X: -4,99
X: -4,98
X: -4,9700000000000000001
How can i fix this by still using doubles?
Secondly i am getting this number at the end of the program: -6.230779781013496E-14
. This is the found null point at X = -6.230779781013496E-14
. How can I convert this number into something like a normal-readable double with 5 characters after the "."?