Hi friend,
How to reduce the square root values of error?
My codes:
using System;
namespace IsaacNewto{
static class INMath{
static INMath() { }////// Newton-Raphson method --Calculate square root.////// The square root of n.///Returns the square root of n. public static double Sqrt(double n){
double inNum1 = n / 2, inNum2;inNum2 = (inNum1 + (n / inNum1)) / 2;do{
inNum1 = inNum2;inNum2 = (inNum1 +n / inNum1) / 2;
} while (Math.Abs(inNum1 - inNum2) >=Math.Pow(10,-5)); // approximate value.return inNum1;}}
class Program{
static void Main(string[] args){
Console.WriteLine(Math.Sqrt(36.0));Console.WriteLine(INMath.Sqrt(36.0));
}
}
}
Thanks.

VulpesPosted Jul 25, 2014, 11:59 AM
As the precision of doubles is 15 to 16 digits, I'd use:
while (Math.Abs(inNum1 - inNum2) >=Math.Pow(10,-15)); // changed -5 to -15
This now gives a square root of 6 using Newton-Raphson.
Ken HPosted Jul 25, 2014, 11:25 PM