- /// <summary>
- /// Cuberoot运算
- /// </summary>
- /// <param name="a">输入参数List,正确输入时仅含一个数据</param>
- /// <returns>返回运算结果</returns>
- public static BigDecimal Cuberoot(List<BigDecimal> a)
- {
- if (a.Count() != 1)
- {
- throw new Exception("cuberoot函数输入错误");
- }
- else
- {
- BigDecimal Num = BigDecimal.Simplify(a.First());
- //若Num的整数部分为零,则直接返回它本身
- if (Num.GetIntPart() == 0)
- {
- return Num;
- }
- else
- {
- //使用迭代法求立方根
- //定义常数3
- BigDecimal Three = new BigDecimal(3);
- //定义计算结果和开立方数Y
- BigDecimal Result = new BigDecimal(0);
- BigDecimal Y = new BigDecimal(Num);
- //先对阶数化简
- Result.SetOrder(Y.GetOrder() / 3);
- Y.SetOrder(Y.GetOrder() % 3);
- BigDecimal CalculateEps = new BigDecimal("1e" + ((BigInteger.Abs(Y.GetIntPart()).ToString().Length + Y.GetOrder()) / 3 - BigDecimal.Accuracy).ToString());
- //先限制乘法有效位数
- BigDecimal.LimitMultiply = true;
- //开始计算
- BigDecimal X = new BigDecimal(1);
- BigDecimal NewX = (X + X + Y / (X * X)) / Three;
- while (BigDecimal.Abs((NewX - X)) > CalculateEps)
- {
- X = NewX;
- NewX = (X + X + Y / (X * X)) / Three;
- }
- Result.SetIntPart(NewX.GetIntPart());
- Result.SetOrder(Result.GetOrder() + NewX.GetOrder());
- Result = BigDecimal.Simplify(Result);
- //关闭限制乘法有效位数
- BigDecimal.LimitMultiply = false;
- return Result;
- }
- }
- }
复制代码
以上,欢迎讨论。 |
|