Skip to content

Commit 43666d8

Browse files
committed
math: Applied remarks
1 parent be118f1 commit 43666d8

File tree

3 files changed

+394
-146
lines changed

3 files changed

+394
-146
lines changed

lib/math/math.go

Lines changed: 122 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
package math
1+
// Copyright 2021 The Bazel Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package math provides basic constants and mathematical functions.
6+
package math // import "go.starlark.net/lib/math"
27

38
import (
49
"math"
@@ -7,15 +12,22 @@ import (
712
"go.starlark.net/starlarkstruct"
813
)
914

10-
const tau = math.Pi * 2
11-
const oneRad = tau / 360
15+
const (
16+
tau = math.Pi * 2
17+
oneRad = tau / 360
18+
)
19+
20+
var (
21+
toDeg = func(x float64) float64 { return x / oneRad }
22+
toRad = func(x float64) float64 { return x * oneRad }
23+
)
1224

1325
// Module math is a Starlark module of math-related functions.
1426
var Module = &starlarkstruct.Module{
1527
Name: "math",
1628
Members: starlark.StringDict{
29+
"abs": starlark.NewBuiltin("abs", abs),
1730
"ceil": starlark.NewBuiltin("ceil", ceil),
18-
"fabs": starlark.NewBuiltin("fabs", fabs),
1931
"floor": starlark.NewBuiltin("floor", floor),
2032
"round": starlark.NewBuiltin("round", round),
2133

@@ -42,59 +54,144 @@ var Module = &starlarkstruct.Module{
4254
"tanh": starlark.NewBuiltin("tanh", tanh),
4355

4456
"e": starlark.Float(math.E),
45-
"pi": starlark.Float(math.Pi),
46-
"tau": starlark.Float(tau),
4757
"phi": starlark.Float(math.Phi),
48-
"inf": starlark.Float(math.Inf(1)),
49-
"nan": starlark.Float(math.NaN()),
58+
"pi": starlark.Float(math.Pi),
5059
},
5160
}
5261

53-
// floatFunc unpacks a starlark function call, calls a passed in float64 function
62+
// oneArgFunc unpacks a starlark function call with one argument, calls a passed in function taking one float64 as argument
5463
// and returns the result as a starlark value
55-
func floatFunc(name string, args starlark.Tuple, kwargs []starlark.Tuple, fn func(float64) float64) (starlark.Value, error) {
56-
var x starlark.Float
64+
func oneArgFunc(name string, args starlark.Tuple, kwargs []starlark.Tuple, fn func(float64) float64) (starlark.Value, error) {
65+
var x float64
5766
if err := starlark.UnpackPositionalArgs(name, args, kwargs, 1, &x); err != nil {
58-
return nil, err
67+
var i int64
68+
if starlark.UnpackPositionalArgs(name, args, kwargs, 1, &i) != nil {
69+
return nil, err
70+
}
71+
x = float64(i)
5972
}
60-
return starlark.Float(fn(float64(x))), nil
73+
return starlark.Float(fn(x)), nil
6174
}
6275

63-
// floatFunc2 is a 2-argument float func
64-
func floatFunc2(name string, args starlark.Tuple, kwargs []starlark.Tuple, fn func(float64, float64) float64) (starlark.Value, error) {
65-
var x, y starlark.Float
76+
// twoArgFunc unpacks a starlark function call with two arguments, calls a passed in function taking two float64 as argument
77+
// and returns the result as a starlark value
78+
func twoArgFunc(name string, args starlark.Tuple, kwargs []starlark.Tuple, fn func(float64, float64) float64) (starlark.Value, error) {
79+
var x, y float64
6680
if err := starlark.UnpackPositionalArgs(name, args, kwargs, 2, &x, &y); err != nil {
6781
return nil, err
6882
}
69-
return starlark.Float(fn(float64(x), float64(y))), nil
83+
return starlark.Float(fn(x, y)), nil
7084
}
7185

72-
// Return the floor of x, the largest integer less than or equal to x.
86+
// floor returns the floor of x, the largest integer less than or equal to x.
7387
func floor(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
74-
return floatFunc("floor", args, kwargs, math.Floor)
88+
return oneArgFunc("floor", args, kwargs, math.Floor)
7589
}
7690

7791
// Return the ceiling of x, the smallest integer greater than or equal to x
7892
func ceil(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
79-
return floatFunc("ceil", args, kwargs, math.Ceil)
93+
return oneArgFunc("ceil", args, kwargs, math.Ceil)
8094
}
8195

8296
// Return the absolute value of x
83-
func fabs(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
84-
return floatFunc("fabs", args, kwargs, math.Abs)
97+
func abs(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
98+
return oneArgFunc("abs", args, kwargs, math.Abs)
8599
}
86100

87101
// Round returns the nearest integer, rounding half away from zero.
88102
func round(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
89-
return floatFunc("fabs", args, kwargs, math.Round)
103+
return oneArgFunc("round", args, kwargs, math.Round)
90104
}
91105

92106
// Return e raised to the power x, where e = 2.718281… is the base of natural logarithms.
93107
func exp(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
94-
return floatFunc("exp", args, kwargs, math.Exp)
108+
return oneArgFunc("exp", args, kwargs, math.Exp)
95109
}
96110

97111
// Return the square root of x
98112
func sqrt(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
99-
return floatFunc("sqrt", args, kwargs, math.Sqrt)
113+
return oneArgFunc("sqrt", args, kwargs, math.Sqrt)
114+
}
115+
116+
// Return the arc cosine of x, in radians.
117+
func acos(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
118+
return oneArgFunc("acos", args, kwargs, math.Acos)
119+
}
120+
121+
// asin(x) - Return the arc sine of x, in radians.
122+
func asin(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
123+
return oneArgFunc("asin", args, kwargs, math.Asin)
124+
}
125+
126+
// atan(x) - Return the arc tangent of x, in radians.
127+
func atan(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
128+
return oneArgFunc("atan", args, kwargs, math.Atan)
129+
}
130+
131+
// atan2(y, x) - Return atan(y / x), in radians. The result is between -pi and pi.
132+
// The vector in the plane from the origin to point (x, y) makes this angle with the positive X axis.
133+
// The point of atan2() is that the signs of both inputs are known to it, so it can compute the correct quadrant for the angle.
134+
// For example, atan(1) and atan2(1, 1) are both pi/4, but atan2(-1, -1) is -3*pi/4.
135+
func atan2(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
136+
return twoArgFunc("atan2", args, kwargs, math.Atan2)
137+
}
138+
139+
// cos(x) - Return the cosine of x radians.
140+
func cos(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
141+
return oneArgFunc("cos", args, kwargs, math.Cos)
142+
}
143+
144+
// hypot(x, y) - Return the Euclidean norm, sqrt(x*x + y*y). This is the length of the vector from the origin to point (x, y).
145+
func hypot(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
146+
return twoArgFunc("hypot", args, kwargs, math.Hypot)
147+
}
148+
149+
// sin(x) - Return the sine of x radians.
150+
func sin(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
151+
return oneArgFunc("sin", args, kwargs, math.Sin)
152+
}
153+
154+
// tan(x) - Return the tangent of x radians.
155+
func tan(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
156+
return oneArgFunc("tan", args, kwargs, math.Tan)
157+
}
158+
159+
// degrees(x) - Convert angle x from radians to degrees.
160+
func degrees(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
161+
return oneArgFunc("degrees", args, kwargs, toDeg)
162+
}
163+
164+
// radians(x) - Convert angle x from degrees to radians.
165+
func radians(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
166+
return oneArgFunc("radians", args, kwargs, toRad)
167+
}
168+
169+
// acosh(x) - Return the inverse hyperbolic cosine of x.
170+
func acosh(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
171+
return oneArgFunc("acosh", args, kwargs, math.Acosh)
172+
}
173+
174+
// asinh(x) - Return the inverse hyperbolic sine of x.
175+
func asinh(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
176+
return oneArgFunc("asinh", args, kwargs, math.Asinh)
177+
}
178+
179+
// atanh(x) - Return the inverse hyperbolic tangent of x.
180+
func atanh(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
181+
return oneArgFunc("atanh", args, kwargs, math.Atanh)
182+
}
183+
184+
// cosh(x) - Return the hyperbolic cosine of x.
185+
func cosh(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
186+
return oneArgFunc("cosh", args, kwargs, math.Cosh)
187+
}
188+
189+
// sinh(x) - Return the hyperbolic sine of x.
190+
func sinh(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
191+
return oneArgFunc("sinh", args, kwargs, math.Sinh)
192+
}
193+
194+
// tanh(x) - Return the hyperbolic tangent of x.
195+
func tanh(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
196+
return oneArgFunc("tanh", args, kwargs, math.Tanh)
100197
}

lib/math/trig.go

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)