Skip to content

Commit 2a9903d

Browse files
committed
time: Inline the log function
1 parent 899dc83 commit 2a9903d

File tree

2 files changed

+19
-30
lines changed

2 files changed

+19
-30
lines changed

lib/math/math.go

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import (
5353
// sinh(x) - Returns the hyperbolic sine of x.
5454
// tanh(x) - Returns the hyperbolic tangent of x.
5555
//
56-
// log(x, base=e) - Returns the logarithm of x in the given base, or natural logarithm by default.
56+
// log(x, base) - Returns the logarithm of x in the given base, or natural logarithm by default.
5757
//
5858
// gamma(x) - Returns the Gamma function of x.
5959
//
@@ -98,7 +98,7 @@ var Module = &starlarkstruct.Module{
9898
"sinh": newUnaryBuiltin("sinh", math.Sinh),
9999
"tanh": newUnaryBuiltin("tanh", math.Tanh),
100100

101-
"log": newLogFunction(),
101+
"log": starlark.NewBuiltin("log", log),
102102

103103
"gamma": newUnaryBuiltin("gamma", math.Gamma),
104104

@@ -146,23 +146,20 @@ func newBinaryBuiltin(name string, fn func(float64, float64) float64) *starlark.
146146
})
147147
}
148148

149-
// newLogFunction wraps the Log function
149+
// log wraps the Log function
150150
// as a Starlark built-in that accepts int or float arguments.
151-
func newLogFunction() *starlark.Builtin {
152-
return starlark.NewBuiltin("log", func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
153-
var (
154-
x floatOrInt
155-
base = floatOrInt(math.E)
156-
)
157-
if err := starlark.UnpackArgs("log", args, kwargs, "x", &x, "base?", &base); err != nil {
158-
return nil, err
159-
}
160-
result, err := log(float64(x), float64(base))
161-
if err != nil {
162-
return nil, err
163-
}
164-
return starlark.Float(result), nil
165-
})
151+
func log(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
152+
var (
153+
x floatOrInt
154+
base floatOrInt = math.E
155+
)
156+
if err := starlark.UnpackPositionalArgs("log", args, kwargs, 1, &x, &base); err != nil {
157+
return nil, err
158+
}
159+
if base == 1 {
160+
return nil, errors.New("division by zero")
161+
}
162+
return starlark.Float(math.Log(float64(x)) / math.Log(float64(base))), nil
166163
}
167164

168165
func degrees(x float64) float64 {
@@ -172,10 +169,3 @@ func degrees(x float64) float64 {
172169
func radians(x float64) float64 {
173170
return 2 * math.Pi * x / 360
174171
}
175-
176-
func log(x float64, base float64) (float64, error) {
177-
if base == 1 {
178-
return 0, errors.New("division by zero")
179-
}
180-
return math.Log(x) / math.Log(base), nil
181-
}

starlark/testdata/math.star

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,18 +335,17 @@ assert.eq(math.tanh(nan), nan)
335335
assert.fails(lambda: math.tanh("0"), "got string, want float or int")
336336
# log
337337
assert.eq(math.log(math.e), 1)
338-
assert.eq(math.log(10, base=10), 1)
338+
assert.eq(math.log(10, 10), 1)
339339
assert.eq(math.log(10.0, 10.0), 1)
340-
assert.eq(math.log(2, base=2.0), 1)
341-
assert.fails(lambda: math.log(2, base=1), "division by zero")
342-
assert.fails(lambda: math.log(0.99, base=1.0), "division by zero")
340+
assert.eq(math.log(2, 2.0), 1)
341+
assert.fails(lambda: math.log(2, 1), "division by zero")
342+
assert.fails(lambda: math.log(0.99, 1.0), "division by zero")
343343
assert.eq(math.log(0.0), -inf)
344344
assert.eq(math.log(0), -inf)
345345
assert.eq(math.log(-1.0), nan)
346346
assert.eq(math.log(-1), nan)
347347
assert.eq(math.log(nan), nan)
348348
assert.fails(lambda: math.log("0"), "got string, want float or int")
349-
assert.fails(lambda: math.log(10, base="10"), "got string, want float or int")
350349
assert.fails(lambda: math.log(10, "10"), "got string, want float or int")
351350
# gamma
352351
assert.eq(math.gamma(1.0), 1)

0 commit comments

Comments
 (0)