簡體   English   中英

如何在C#中使用Newton-Raphson方法查找BigInteger的平方根

[英]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?

您使用了錯誤的數據類型。 為了擁有小數點,您將需要使用doublefloatdecimalComplex

檢查所有這些類型的鏈接,以便您可以查看它們的精度數字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM