Skip to content

Commit

Permalink
Adapt tests to use different precision than abort condition for betac…
Browse files Browse the repository at this point in the history
…f() function
  • Loading branch information
fako1024 committed Apr 3, 2023
1 parent 3697f57 commit c71fffc
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/fako1024/numerics

go 1.14
go 1.18
Empty file added go.sum
Empty file.
2 changes: 1 addition & 1 deletion numerics.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func betacf(x, a, b float64) float64 {
h *= hfac

// If sufficient precision is reached, return
if math.Abs(hfac-1) < betaEpsilon {
if math.Abs(hfac-1.0) < betaEpsilon {
return h
}
}
Expand Down
12 changes: 8 additions & 4 deletions numerics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"testing"
)

const (
testEpsilon = 3e-8
)

func TestBetaComplete(t *testing.T) {

type testCaseBetaComplete struct {
Expand Down Expand Up @@ -37,7 +41,7 @@ func TestBetaComplete(t *testing.T) {
}

for _, cs := range testTableBetaComplete {
if beta := Beta(cs.a, cs.b); math.Abs(beta-cs.expected) > betaEpsilon {
if beta := Beta(cs.a, cs.b); math.Abs(beta-cs.expected) > testEpsilon {
t.Fatalf("Test driven call to Beta failed (a=%.3f, b=%.3f), want %.19f, have %.19f", cs.a, cs.b, cs.expected, beta)
}
}
Expand Down Expand Up @@ -103,11 +107,11 @@ func TestBetaIncomplete(t *testing.T) {
}

for _, cs := range testTableBetaIncomplete {
if betaReg := BetaIncompleteRegular(cs.x, cs.a, cs.b); math.Abs(betaReg-cs.expected) > betaEpsilon {
if betaReg := BetaIncompleteRegular(cs.x, cs.a, cs.b); math.Abs(betaReg-cs.expected) > testEpsilon {
t.Fatalf("Test driven call to BetaIncompleteRegular failed (x=%.3f, a=%.3f, b=%.3f), want %.19f, have %.19f", cs.x, cs.a, cs.b, cs.expected, betaReg)
}

if beta := BetaIncomplete(cs.x, cs.a, cs.b); math.Abs(beta-cs.expected*Beta(cs.a, cs.b)) > betaEpsilon {
if beta := BetaIncomplete(cs.x, cs.a, cs.b); math.Abs(beta-cs.expected*Beta(cs.a, cs.b)) > testEpsilon {
t.Fatalf("Test driven call to BetaIncomplete failed (x=%.3f, a=%.3f, b=%.3f), want %.19f, have %.19f", cs.x, cs.a, cs.b, cs.expected*Beta(cs.a, cs.b), beta)
}
}
Expand Down Expand Up @@ -173,7 +177,7 @@ func TestBinomial(t *testing.T) {
}

for _, cs := range testTableBinomial {
if binomial := Binomial(cs.x, cs.a-1., cs.b); math.Abs(binomial-cs.expected) > betaEpsilon {
if binomial := Binomial(cs.x, cs.a-1., cs.b); math.Abs(binomial-cs.expected) > testEpsilon {
t.Fatalf("Test driven call to Binomial failed (x=%.3f, a=%.3f, b=%.3f), want %.19f, have %.19f", cs.x, cs.a, cs.b, cs.expected, binomial)
}
}
Expand Down

0 comments on commit c71fffc

Please sign in to comment.