Skip to content

Commit 90ee173

Browse files
committed
Fixes for Log function
1 parent 23b5732 commit 90ee173

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

function_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ func TestLn(t *testing.T) {
6868

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

7373
val = gal.Log(gal.NewNumber(-123456), gal.NewNumber(5))
7474
assert.Equal(t, "undefined: Log:cannot calculate natural logarithm for negative decimals", val.String())
75+
76+
val = gal.Log(gal.NewNumber(10_000_000), gal.NewNumber(0))
77+
assert.Equal(t, "7", val.String())
7578
}

gal_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,6 @@ func TestNestedFunctions(t *testing.T) {
266266
// If renaming this test, also update the README.md file, where it is mentioned.
267267
func TestMultiValueFunctions(t *testing.T) {
268268
expr := `sum(div(triple(7) double(4)))`
269-
parsedExpr := gal.Parse(expr)
270269

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

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

331-
got := parsedExpr.Eval(
330+
got := gal.Parse(expr).Eval(
332331
gal.WithFunctions(funcs),
333332
)
334333
expected := gal.NewNumber(7)

value.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -371,18 +371,18 @@ func (n Number) Ln(precision int32) Value {
371371
}
372372

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

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

384384
return Number{
385-
value: res1.Div(res10).Truncate(precision),
385+
value: res.Div(res10).Truncate(precision),
386386
}
387387
}
388388

0 commit comments

Comments
 (0)