Skip to content

Commit

Permalink
Fixes for Log function
Browse files Browse the repository at this point in the history
  • Loading branch information
seborama committed May 7, 2024
1 parent 23b5732 commit 90ee173
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
5 changes: 4 additions & 1 deletion function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ func TestLn(t *testing.T) {

func TestLog(t *testing.T) {
val := gal.Log(gal.NewNumber(123456), gal.NewNumber(5))
assert.Equal(t, "5.09152", val.String())
assert.Equal(t, "5.09151", val.String())

val = gal.Log(gal.NewNumber(-123456), gal.NewNumber(5))
assert.Equal(t, "undefined: Log:cannot calculate natural logarithm for negative decimals", val.String())

val = gal.Log(gal.NewNumber(10_000_000), gal.NewNumber(0))
assert.Equal(t, "7", val.String())
}
5 changes: 2 additions & 3 deletions gal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ func TestNestedFunctions(t *testing.T) {
// If renaming this test, also update the README.md file, where it is mentioned.
func TestMultiValueFunctions(t *testing.T) {
expr := `sum(div(triple(7) double(4)))`
parsedExpr := gal.Parse(expr)

// step 1: define funcs and vars and Eval the expression
funcs := gal.Functions{
Expand Down Expand Up @@ -297,7 +296,7 @@ func TestMultiValueFunctions(t *testing.T) {
"div": func(args ...gal.Value) gal.Value {
// returns the division of value1 by value2 as the interger portion and the remainder
if len(args) != 2 {
return gal.NewUndefinedWithReasonf("mult() requires two arguments, got %d", len(args))
return gal.NewUndefinedWithReasonf("div() requires two arguments, got %d", len(args))
}

dividend := args[0].(gal.Numberer).Number()
Expand Down Expand Up @@ -328,7 +327,7 @@ func TestMultiValueFunctions(t *testing.T) {
},
}

got := parsedExpr.Eval(
got := gal.Parse(expr).Eval(
gal.WithFunctions(funcs),
)
expected := gal.NewNumber(7)
Expand Down
6 changes: 3 additions & 3 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,18 +371,18 @@ func (n Number) Ln(precision int32) Value {
}

func (n Number) Log(precision int32) Value {
res1, err := n.value.Ln(precision)
res, err := n.value.Ln(precision + 1)
if err != nil {
return NewUndefinedWithReasonf("Log:" + err.Error())
}

res10, err := decimal.New(10, 0).Ln(precision)
res10, err := decimal.New(10, 0).Ln(precision + 1)
if err != nil {
return NewUndefinedWithReasonf("Log:" + err.Error())
}

return Number{
value: res1.Div(res10).Truncate(precision),
value: res.Div(res10).Truncate(precision),
}
}

Expand Down

0 comments on commit 90ee173

Please sign in to comment.