C#方法:定义的参数默认值问题

时间:2011-11-08 00:12:08

标签: c# methods parameters default-value named

我正在编写一个需要计算Gamma功能的应用程序。 代码(类的一部分)代码段如下:

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);
    }
}

问题是在编译时,Gamma中的参数x(即使x在调用组件中被赋值为3.5)被赋值为0,触发异常。任何人都可以建议我如何解决这个问题?谢谢。

1 个答案:

答案 0 :(得分: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)