c#用牛顿法计算根号下2的值
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 牛顿法计算根号下2的值 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 double a = 0, b = 2, t = 0; 14 while (b - a > 0.0000001) 15 { 16 t = (a + b) / 2; 17 if (t * t - 2 == 0) 18 { 19 Console.WriteLine(t); 20 break; 21 } 22 else if ((t * t - 2) > 0) 23 { 24 b = t; 25 Console.WriteLine(t * t - 2); 26 } 27 else 28 { 29 a = t; 30 Console.WriteLine(t * t - 2); 31 } 32 } 33 Console.WriteLine("sqt(2)={0}",t); 34 } 35 } 36 }
http://www.51taoshi.com/ucenter/pub/diaryShow.action?did=140525134206646418