2014-01-20 60 views
-4

如何在c#中使用此公式?如何轉換C#中的字符串公式?

double Diameter = 160; 

double Height = 118.2; 

double pi = 3.1416 

雙重結果=

enter image description here

+1

見類'System.Math'。 –

+1

你嘗試了什麼?你不明白什麼?你在問如何增加數字? – SLaks

+0

檢查System.Math-class它幾乎包含了你需要的所有東西。否則,您可以查找像MathUtils這樣的庫: https://github.com/Artentus/MathUtils –

回答

0

首先你read the documentation和相當多的代碼它作爲書面:

public static double ComputeResult(double diameter , double height) 
{ 
    double result = 0.5 
        * (
          ((2.0*height) - diameter) 
         * Math.Sqrt((height*diameter) - Math.Pow(height,2.0)) 
         + (diameter/2.0) 
         * Math.Asin(2.0*height -1.0) 
         /diameter 
         + (Math.PI * Math.Pow(diameter,2.0)) 
         /2.0 
        ) ; 
    return result ; 
} 

雖然你的公式似乎吐出NaN(非數字)頗有幾分。

如果您重構它以單獨評估每個中間計算並隨着時間逐步建立結果,那麼檢查計算會更容易,就像在紙張/黑板/幻燈片規則/計算器上工作時一樣。

你就是我的計算,相加併除以2,基本3乘表情,所以你可以把它分解成至少3個(雖然我可能會走的更遠):

public static double ComputeResult(double diameter , double height) 
{ 
    double t1 = ((2.0*height) - diameter) 
      * Math.Sqrt((height*diameter) - Math.Pow(height,2.0)) 
      ; 
    double t2 = (diameter/2.0) 
      * Math.Asin(2.0*height-1.0) 
      /diameter 
      ; 
    double t3 = (Math.PI * Math.Pow(diameter,2.0)) 
      /2.0 
      ; 
    double result = 0.5 * (t1 + t2 + t3) ; 
    return result ; 
} 
+0

非常感謝你 – user3107343

+0

結果返回NaN.What是什麼意思? – user3107343

+0

Nan =不是數字。標準浮點錯誤條件。例如除以零,取負數的平方根,試圖得到域x以外的值x的反正弦-1 <= x <= +1等將生成「Nan」。 'NaN'傳播。請參閱http://en.wikipedia.org/wiki/NaN#Operations_generating_NaN。正如其他人指出你的配方似乎有一些問題。 –