![](/img/trans.png)
[英]math.sqrt vs. Newton-Raphson Method for finding roots in c#
[英]How to use Newton-Raphson method to find the square root of a BigInteger in C#
因此,我嘗試使用Newton-Raphson方法查找BigInteger的平方根。
這是我的代碼:
private void sqrRt(BigInteger candidate)
{
BigInteger epsilon = new BigInteger(0.0001);
BigInteger guess = candidate / 2;
while (BigInteger.Abs(guess * guess - candidate) >= epsilon)
{
// guess = guess - (((guess**2) - y)/(2*guess))
guess = BigInteger.Subtract(guess, BigInteger.Divide(BigInteger.Subtract(BigInteger.Multiply(guess, guess), candidate), BigInteger.Multiply(2, guess)));
MessageBox.Show(Convert.ToString(guess));
}
}
問題似乎在於,BigInteger的精度不足以使其處於while循環中epsilon的精度范圍內-即,它需要一個小數位。 我的問題是什么/如何/在哪里轉換為雙精度值,以使while循環最終返回false?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.