ref #65, new formula functions: GAMMA.DIST and GAMMADIST

This commit is contained in:
xuri 2022-03-11 00:07:30 +08:00
parent aee7bdd3a0
commit 361611c23a
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
2 changed files with 74 additions and 0 deletions

50
calc.go
View File

@ -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:
//

View File

@ -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",