From 361611c23ae56cac091d5c7d73f5ea305b5bd3fc Mon Sep 17 00:00:00 2001 From: xuri Date: Fri, 11 Mar 2022 00:07:30 +0800 Subject: ref #65, new formula functions: GAMMA.DIST and GAMMADIST --- calc.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ calc_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/calc.go b/calc.go index 2d1aa0e..1f1408e 100644 --- a/calc.go +++ b/calc.go @@ -430,6 +430,8 @@ type formulaFuncs struct { // FV // FVSCHEDULE // GAMMA +// GAMMA.DIST +// GAMMADIST // GAMMALN // GAUSS // GCD @@ -6038,6 +6040,54 @@ func (fn *formulaFuncs) GAMMA(argsList *list.List) formulaArg { return newErrorFormulaArg(formulaErrorVALUE, "GAMMA requires 1 numeric argument") } +// GAMMAdotDIST function returns the Gamma Distribution, which is frequently +// used to provide probabilities for values that may have a skewed +// distribution, such as queuing analysis. +// +// GAMMA.DIST(x,alpha,beta,cumulative) +// +func (fn *formulaFuncs) GAMMAdotDIST(argsList *list.List) formulaArg { + if argsList.Len() != 4 { + return newErrorFormulaArg(formulaErrorVALUE, "GAMMA.DIST requires 4 arguments") + } + return fn.GAMMADIST(argsList) +} + +// GAMMADIST function returns the Gamma Distribution, which is frequently used +// to provide probabilities for values that may have a skewed distribution, +// such as queuing analysis. +// +// GAMMADIST(x,alpha,beta,cumulative) +// +func (fn *formulaFuncs) GAMMADIST(argsList *list.List) formulaArg { + if argsList.Len() != 4 { + return newErrorFormulaArg(formulaErrorVALUE, "GAMMADIST requires 4 arguments") + } + var x, alpha, beta, cumulative formulaArg + if x = argsList.Front().Value.(formulaArg).ToNumber(); x.Type != ArgNumber { + return x + } + if x.Number < 0 { + return newErrorFormulaArg(formulaErrorNUM, formulaErrorNUM) + } + if alpha = argsList.Front().Next().Value.(formulaArg).ToNumber(); alpha.Type != ArgNumber { + return alpha + } + if beta = argsList.Back().Prev().Value.(formulaArg).ToNumber(); beta.Type != ArgNumber { + return beta + } + if alpha.Number <= 0 || beta.Number <= 0 { + return newErrorFormulaArg(formulaErrorNUM, formulaErrorNUM) + } + if cumulative = argsList.Back().Value.(formulaArg).ToBool(); cumulative.Type == ArgError { + return cumulative + } + if cumulative.Number == 1 { + return newNumberFormulaArg(incompleteGamma(alpha.Number, x.Number/beta.Number) / math.Gamma(alpha.Number)) + } + return newNumberFormulaArg((1 / (math.Pow(beta.Number, alpha.Number) * math.Gamma(alpha.Number))) * math.Pow(x.Number, (alpha.Number-1)) * math.Exp(0-(x.Number/beta.Number))) +} + // GAMMALN function returns the natural logarithm of the Gamma Function, Γ // (n). The syntax of the function is: // diff --git a/calc_test.go b/calc_test.go index 9804cc9..0c9fb2e 100644 --- a/calc_test.go +++ b/calc_test.go @@ -835,6 +835,12 @@ func TestCalcCellValue(t *testing.T) { "=GAMMA(INT(1))": "1", "=GAMMA(1.5)": "0.886226925452758", "=GAMMA(5.5)": "52.34277778455352", + // GAMMA.DIST + "=GAMMA.DIST(6,3,2,FALSE)": "0.112020903827694", + "=GAMMA.DIST(6,3,2,TRUE)": "0.576809918873156", + // GAMMADIST + "=GAMMADIST(6,3,2,FALSE)": "0.112020903827694", + "=GAMMADIST(6,3,2,TRUE)": "0.576809918873156", // GAMMALN "=GAMMALN(4.5)": "2.45373657084244", "=GAMMALN(INT(1))": "0", @@ -2382,6 +2388,24 @@ func TestCalcCellValue(t *testing.T) { "=GAMMA(F1)": "GAMMA requires 1 numeric argument", "=GAMMA(0)": "#N/A", "=GAMMA(INT(0))": "#N/A", + // GAMMA.DIST + "=GAMMA.DIST()": "GAMMA.DIST requires 4 arguments", + "=GAMMA.DIST(\"\",3,2,FALSE)": "strconv.ParseFloat: parsing \"\": invalid syntax", + "=GAMMA.DIST(6,\"\",2,FALSE)": "strconv.ParseFloat: parsing \"\": invalid syntax", + "=GAMMA.DIST(6,3,\"\",FALSE)": "strconv.ParseFloat: parsing \"\": invalid syntax", + "=GAMMA.DIST(6,3,2,\"\")": "strconv.ParseBool: parsing \"\": invalid syntax", + "=GAMMA.DIST(-1,3,2,FALSE)": "#NUM!", + "=GAMMA.DIST(6,0,2,FALSE)": "#NUM!", + "=GAMMA.DIST(6,3,0,FALSE)": "#NUM!", + // GAMMADIST + "=GAMMADIST()": "GAMMADIST requires 4 arguments", + "=GAMMADIST(\"\",3,2,FALSE)": "strconv.ParseFloat: parsing \"\": invalid syntax", + "=GAMMADIST(6,\"\",2,FALSE)": "strconv.ParseFloat: parsing \"\": invalid syntax", + "=GAMMADIST(6,3,\"\",FALSE)": "strconv.ParseFloat: parsing \"\": invalid syntax", + "=GAMMADIST(6,3,2,\"\")": "strconv.ParseBool: parsing \"\": invalid syntax", + "=GAMMADIST(-1,3,2,FALSE)": "#NUM!", + "=GAMMADIST(6,0,2,FALSE)": "#NUM!", + "=GAMMADIST(6,3,0,FALSE)": "#NUM!", // GAMMALN "=GAMMALN()": "GAMMALN requires 1 numeric argument", "=GAMMALN(F1)": "GAMMALN requires 1 numeric argument", -- cgit v1.2.1