@@ -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
168165func degrees (x float64 ) float64 {
@@ -172,10 +169,3 @@ func degrees(x float64) float64 {
172169func 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- }
0 commit comments