二分法求常数E,常数π,根号2

进行单元测试数据

____________________________________


public
class Test { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(bin(-1000000000,9999,0.00000001)); } //提供的接口 //区间[a,b] 和 区间精度d static double bin(double a,double b,double d){ while(true){ double mid = (a+b)/2; //第一步 if(f3(a)*f3(mid)<0) //若 :第二步 b=mid; else a=mid; if(f3(mid)==0 || b-a<=d)//若 :第三步 return (a+b)/2; } } static boolean judge(double a,double b){ if(a*b<0) return true; else return false; } static double f1(double x){ // y=x^2 -2 输入x得到函数值y return x*x - 2; } static double f2(double x){ // y=sinx return Math.sin(x); } static double f3(double x){ // y=x-e return x-Math.E; } }
原文地址:https://www.cnblogs.com/cs-lcy/p/7487200.html