Skip to content

Commit f8a3101

Browse files
committed
math: Applied remarks
1 parent be118f1 commit f8a3101

File tree

3 files changed

+402
-153
lines changed

3 files changed

+402
-153
lines changed

lib/math/math.go

Lines changed: 130 additions & 32 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+
)
1219

13-
// Module math is a Starlark module of math-related functions.
20+
var (
21+
toDeg = func(x float64) float64 { return x / oneRad }
22+
toRad = func(x float64) float64 { return x * oneRad }
23+
)
24+
25+
// Module math is a Starlark module of math-related functions and constants.
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,145 @@ 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
54-
// 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
62+
// oneArgFunc unpacks a starlark function call with one argument, calls a passed in function taking one float64 as argument
63+
// and returns the result as a starlark value.
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(x) - Return 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

77-
// Return the ceiling of x, the smallest integer greater than or equal to x
91+
// ceil(x) - 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

82-
// 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)
96+
// abs(x) - Return the absolute value of x.
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

87-
// Round returns the nearest integer, rounding half away from zero.
101+
// round(x) - 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

92-
// Return e raised to the power x, where e = 2.718281… is the base of natural logarithms.
106+
// exp(x) - 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

97-
// Return the square root of x
111+
// sqrt(x) - 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+
// acos(x) - 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.
132+
// The result is between -pi and pi.
133+
// The vector in the plane from the origin to point (x, y) makes this angle with the positive X axis.
134+
// 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.
135+
// For example, atan(1) and atan2(1, 1) are both pi/4, but atan2(-1, -1) is -3*pi/4.
136+
func atan2(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
137+
return twoArgFunc("atan2", args, kwargs, math.Atan2)
138+
}
139+
140+
// cos(x) - Return the cosine of x, in radians.
141+
func cos(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
142+
return oneArgFunc("cos", args, kwargs, math.Cos)
143+
}
144+
145+
// 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).
146+
func hypot(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
147+
return twoArgFunc("hypot", args, kwargs, math.Hypot)
148+
}
149+
150+
// sin(x) - Return the sine of x, in radians.
151+
func sin(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
152+
return oneArgFunc("sin", args, kwargs, math.Sin)
153+
}
154+
155+
// tan(x) - Return the tangent of x, in radians.
156+
func tan(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
157+
return oneArgFunc("tan", args, kwargs, math.Tan)
158+
}
159+
160+
// degrees(x) - Convert angle x from radians to degrees.
161+
func degrees(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
162+
return oneArgFunc("degrees", args, kwargs, toDeg)
163+
}
164+
165+
// radians(x) - Convert angle x from degrees to radians.
166+
func radians(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
167+
return oneArgFunc("radians", args, kwargs, toRad)
168+
}
169+
170+
// acosh(x) - Return the inverse hyperbolic cosine of x.
171+
func acosh(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
172+
return oneArgFunc("acosh", args, kwargs, math.Acosh)
173+
}
174+
175+
// asinh(x) - Return the inverse hyperbolic sine of x.
176+
func asinh(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
177+
return oneArgFunc("asinh", args, kwargs, math.Asinh)
178+
}
179+
180+
// atanh(x) - Return the inverse hyperbolic tangent of x.
181+
func atanh(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
182+
return oneArgFunc("atanh", args, kwargs, math.Atanh)
183+
}
184+
185+
// cosh(x) - Return the hyperbolic cosine of x.
186+
func cosh(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
187+
return oneArgFunc("cosh", args, kwargs, math.Cosh)
188+
}
189+
190+
// sinh(x) - Return the hyperbolic sine of x.
191+
func sinh(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
192+
return oneArgFunc("sinh", args, kwargs, math.Sinh)
193+
}
194+
195+
// tanh(x) - Return the hyperbolic tangent of x.
196+
func tanh(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
197+
return oneArgFunc("tanh", args, kwargs, math.Tanh)
100198
}

lib/math/trig.go

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

0 commit comments

Comments
 (0)