C# math.pow cube root calculation

萝らか妹 提交于 2020-01-07 02:52:20

问题


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.

  1. 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.

  2. Secondly, there is an issue with the parenthetical groupings within the expression causing a invalid evaluation of the expression

  3. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!