问题
How can I calculate a cube root of (0.015*(0.05*0.05))
?
I tried the following solutions:
double result = Math.Pow(0.015 * (0.05 * 0.05), 1.0/3.0);
and I am getting 0.03347
.
The same calculation from Volframalpha: 0.015*(0.05*0.05)^0.33
gives 0.00207
.
What am I doing wrong here ?
回答1:
There are three concerns that I have.
You are utilizing Math.Pow() in one part of your expression, however, you will want to use Math.Sqrt() in the first part of the expression which was later given during the conversation.
Secondly, there is an issue with the parenthetical groupings within the expression causing a invalid evaluation of the expression
Thirdly, you will need to use the 'd' character suffix after your numerical values that do not have a decimal value to evaluate the expected result.
The equation: (0.3d * ((0.0015d * (0.793700526d + Math.Sqrt(0.7071068))) + (0.015d * Math.Pow((0.05d * 0.05d), (1d/3d)))))
The code:
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);
}
}
}
Importantly, by convention you will want to use a 'd' suffix after each number.
来源:https://stackoverflow.com/questions/38942580/c-sharp-math-pow-cube-root-calculation