Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6784795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:02:34+00:00 2026-05-26T17:02:34+00:00

I am writing an app that requires the calculation of the Gamma function. A

  • 0

I am writing an app that requires the calculation of the Gamma function.
A code (part of a class) snippet is below:

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

The issue is that at compilation, the parameter x in Gamma (even though x is being assigned the value 3.5 in the calling component) is assigned a value of 0 which triggers the exception. Can anyone please suggest how I can get around this? Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:02:34+00:00Added an answer on May 26, 2026 at 5:02 pm

    Seems to be 3.5 in my test cases. Are you sure you haven’t excluded some information that might be the issue?

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

    Result

    3.5
    

    UPDATED

    The Error is caused by your Function.Gamma(1 / 7, out GammaLn3). This is because both 1 and 7 are INT and dividing (int)1 by (int)7 is zero. Try Function.Gamma(1f / 7f, out GammaLn3).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an android app that requires that I retrieve the html code of
i'm writing an app that needs a calculation support (Fun project). However, is there
I am writing an app that requires you to be logged in to a
I am writing an app that requires the user's current location (lastknownlocation won't be
I'm writing an app that requires a TimePicker-based preference (two, actually) and I swiped
I'm writing a small app that requires a few listboxes, buttons, textboxes. It'll be
I am writing an iPhone app that has a number of UITextFields that requires
I am writing a simple salesforce app that requires app-level and user-level configuration. I
I am writing an App that requires some kind of outside input which I
I am writing an app in Perl that requires long data type instead of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.