【问题标题】:C# math.pow cube root calculationC# math.pow 立方根计算
【发布时间】:2016-08-14 13:11:49
【问题描述】:

如何计算(0.015*(0.05*0.05)) 的立方根?

我尝试了以下解决方案:

double result = Math.Pow(0.015 * (0.05 * 0.05), 1.0/3.0);

我收到0.03347。 Volframalpha 的相同计算:0.015*(0.05*0.05)^0.33 给出0.00207

我在这里做错了什么?

【问题讨论】:

  • 它们是两种不同的计算方式。
  • 您在第二个示例中缺少括号 (0.015*(0.05*0.05))^0.33
  • 在 WA - (0.015*(0.05*0.05))^(1/3) 上试试这个 - 它会给你和你的 C# 代码一样的结果。
  • @Thomas 你没有。您在 WolframAlpha (etc^0.33) 中输入的公式不准确。 1/3 = 0.33333333333333333...(永远重复)。没有十进制扩展可以准确地表示它。但是如果你input the formula using 1/3 instead,你会得到与 C# 相同的结果。
  • @Thomas:所以这是正确的。您不是在寻找 (0.015*(0.05*0.05)) 的立方根,而是在寻找 (0.05*0.05) 乘以 0.015 的立方根 - 您的括号不匹配

标签: c# cube


【解决方案1】:

我有三个担忧。

  1. 您在表达式的一部分中使用了 Math.Pow(),但是,您需要在表达式的第一部分中使用 Math.Sqrt(),稍后在对话中给出。

  2. 其次,表达式中的括号分组存在问题,导致表达式计算无效

  3. 第三,您需要在没有十进制值的数值后使用“d”字符后缀来评估预期结果。

等式: (0.3d * ((0.0015d * (0.793700526d + Math.Sqrt(0.7071068))) + (0.015d * Math.Pow((0.05d * 0.05d), (1d/3d)))))

代码:

using System;

namespace POW
{
    class Program
    {
        static void Main(string[] args)
        {
            // Corrected calculation derived from comment conversation given by author
            double myCalculation1 = (0.3 * ((0.0015 * (0.793700526 + Math.Sqrt(0.7071068))) + (0.015 * Math.Pow((0.05 * 0.05), (1 / 3)))));

            // d suffix used to ensure the non decimal value is treated as a decimal
            double myCalculation2 = (0.3 * ((0.0015 * (0.793700526 + Math.Sqrt(0.7071068))) + (0.015 * Math.Pow((0.05 * 0.05), (1d/3d)))));

            // Output the value of myPow
            Console.WriteLine("The value of myCalculation is: {0}", myCalculation1);
            Console.WriteLine("The value of myCalculation is: {0}", myCalculation2);
        }
    }
}

重要的是,按照惯例,您需要在每个数字后使用“d”后缀。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 2017-02-15
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    相关资源
    最近更新 更多