2011-11-08 11 views
0

ガンマ関数の計算を必要とするアプリケーションを作成しています。 は、コードスニペット(クラスの一部)は以下である:C#メソッド:定義済みパラメータ既定値問題

namespace PB.Utilities.Math 
{ 
// class definition 
public class SpecialFunctions 
{ 
    // Private Fields 

    // Instance Constructor 
    public SpecialFunctions() {} 

    // Public Method for Gamma Function 
    //  x  = input value; x MUST BE > 0 
    //  GammaLn = secondary output value equal to natural log of Gamma Function 
    public double Gamma(double x, out double GammaLn) 
    { 
     try 
     { 
      if (x <= 0) throw new System.ArgumentException("arg <= 0 in GammaFunction", "x"); 
     } 
     catch 
     { 
      System.Console.WriteLine("argument <= 0 in GammaFunction"); 
      System.Console.ReadKey(); 
     } 

     double gammaln; 
     double _gamma = gamma(x, out gammaln); 
     GammaLn = gammaln; 
     return _gamma; 
    } 

    // private method for Gamma Function 
    private double gamma(double xx, out double gammaln) 
    { 
     // private constants 
     int j; 
     double x,tmp,y,ser; 

     const double k1 = 5.24218750000000000; 
     const double k2 = 0.999999999999997092; 
     const double k3 = 2.5066282746310005; 

     double[] cof = new double[14] 
     { 
      57.1562356658629235,  -59.5979603554754912,  14.1360979747417471, 
      -0.491913816097620199,  0.339946499848118887e-4, 0.465236289270485756e-4, 
      -0.983744753048795646e-4, 0.158088703224912494e-3, -0.210264441724104883e-3, 
      0.217439618115212643e-3, -0.164318106536763890e-3, 0.844182239838527433e-4, 
      -0.261908384015814087e-4, 0.368991826595316234e-5 
     }; 

     y = x = xx; 
     tmp = x + k1; 
     tmp = (x + 0.5) * System.Math.Log(tmp) - tmp; 
     ser = k2; 
     for (j = 0; j < 14; j++) ser += cof[j]/++y; 
     gammaln = tmp + System.Math.Log(k3*ser/x); 
     return System.Math.Exp(gammaln); 
    } 
} 
} 

public class BSA 
{ 
    static void Main() 
    { 
     // Create an object of type PB.Utilities.Math.SpecialFunctions 
     PB.Utilities.Math.SpecialFunctions Function = new PB.Utilities.Math.SpecialFunctions(); 

    // Call the public method GammaFunction. 
    double GammaLn1; 
    double GammaLn2; 
    double GammaLn3; 
    double g1 = Function.Gamma(3.5, out GammaLn1); 
    double g2 = Function.Gamma(1.5, out GammaLn2); 
    double g3 = Function.Gamma(1/7, out GammaLn3); 
    System.Console.WriteLine("g(7/2) = "+g1); 
    System.Console.WriteLine("g(3/2) = "+g2); 
    System.Console.WriteLine("g(1/7) = "+g3); 
    } 
} 

問題は、コンパイル時に、ガンマ(xは呼び出し側コンポーネントに値3.5が割り当てられているにもかかわらず)のパラメータxが割り当てられていること例外をトリガする値は0です。誰も私がこれを回避する方法を提案してもらえますか?ありがとうございました。

+0

補足として、 'ArgumentException'の代わりに' ArgumentOutOfRangeException'を投げたいかもしれません。 –

+0

定数を強制的にdoubleとして使用しようとしましたか? 'double g1 = Function.Gamma(3.5f、GammaLn1 out); ' –

+0

あなたが主張しているように、このエラーはコンパイル時には発生しません。 – Icarus

答えて

3

私のテストケースでは3.5と思われます。問題の可能性がある情報を除外していないことをお確かめください。

using System; 

namespace Doubletesting 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      double d = Doubletesting.TestDouble(3.5); 

      Console.WriteLine(d.ToString()); 

      Console.ReadKey(); 
     } 

     public static double TestDouble(double x) 
     { 
      double result; 

      result = x; 

      return result; 
     } 
    } 
} 

結果

3.5 

は、エラーがあなたのFunction.Gamma(1/7, out GammaLn3)によって引き起こされる

を更新しました。これは、1と7の両方がINTであり、(int)1を(int)7で除算することがゼロであるためです。試してくださいFunction.Gamma(1f/7f, out GammaLn3)

+0

hmmm。呼び出し元のコンポーネントは名前空間の外にありますが、それは影響を与えません。私はコードの詳細を投稿しますが、コメントの一部としてどのように行うかはわかりません。 – Zeos6

+0

通常は、元の質問を詳細情報で更新する必要があります。コメントを使用して質問や質問の情報を拡大しないでください。 –

+0

また、デバッグを実行すると、コードはこの例外で停止します – Zeos6

関連する問題