[英]C# math.pow cube root calculation
如何計算(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
。
我在這做錯了什么?
我有三個問題。
您正在表達式的一部分中使用Math.Pow(),但是,您將希望在表達式的第一部分中使用Math.Sqrt(),該部分稍后在對話期間給出。
其次,表達式中的括號分組存在問題,導致對表達式的無效評估
第三,您需要在沒有小數值的數值后使用'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'后綴。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.