2011-11-08 2 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 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로 나누는 것이 0이기 때문입니다. 시도하십시오 Function.Gamma(1f/7f, out GammaLn3).

+0

흠. 호출 구성 요소는 네임 스페이스 외부에 있지만 영향을 미치지 않습니다. 나는 더 많은 코드를 게시 할 것이지만 코멘트의 일부로 그것을하는 방법을 모르겠다. – Zeos6

+0

일반적으로 원래 질문을 더 많은 정보로 업데이트해야합니다. 주석을 사용하여 질문이나 질문 정보를 확장하지 마십시오. –

+0

또한 디버그를 실행하면이 예외에서 코드가 중지됩니다. – Zeos6