0

ガンマ関数の計算を必要とするアプリを作成しています。コード (クラスの一部) スニペットを以下に示します。

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 (呼び出し元のコンポーネントで x に値 3.5 が割り当てられている場合でも) に値 0 が割り当てられ、例外がトリガーされることです。誰かがこれを回避する方法を提案できますか? ありがとうございました。

4

1 に答える 1

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)

于 2011-11-08T00:19:22.017 に答える