Reputation: 2520
For my assignment, I am supposed to use a 'Tolerance Flag'. Yet, I was able to complete the task with what I have so far. What does my teacher mean by tolerance flag?
Use a tolerance flag and make your variables a member of the double class.
const double TOLERANCE = 5e-8;
double x = 2.0;
_
My Work:
public class Sqrt_of_Two {
public static void main(String[] args) {
final double TOLERANCE = 5e-8;
double x = 2;
do{
x = (x + 2/x) / 2;
System.out.println(x);
}while( Math.ceil(x*x) > 2);
}
}
OUTPUT:
1.5
1.4166666666666665
1.4142156862745097
1.4142135623746899
1.414213562373095
Upvotes: 0
Views: 401
Reputation: 328619
I suppose it means something like:
while(Math.abs(x*x - 2) > TOLERANCE);
which outputs:
1.5
1.4166666666666665
1.4142156862745097
1.4142135623746899
Upvotes: 4