










学习研究,记录一下
public void Test() { Console.WriteLine(MyNthRoot(3.1415926)); } /// <summary> /// 自己写的任意数开平方 /// </summary> public static double MyNthRoot(double num, double precision = double.Epsilon) { double x = num; while (true) { double xNew = (x / 2) + num / (2 * x); //1/2+3/2 if (Math.Abs(xNew - x) < precision) { return xNew; } x = xNew; } } /// <summary> /// AI给的任意数开任意次方 /// </summary> public static double NthRoot(double num, int ciFang, double precision = 1e-12) { double x = 1.0; while (true) { double xNew = ((ciFang - 1) * x + num / Math.Pow(x, ciFang - 1)) / ciFang; if (Math.Abs(xNew - x) < precision) return xNew; x = xNew; } }
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。