| 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" |
| 7 |
|
| 8 |
import ( |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"math" |
| 12 |
|
| 13 |
"go.starlark.net/starlark" |
| 14 |
"go.starlark.net/starlarkstruct" |
| 15 |
) |
| 16 |
|
| 17 |
// Module math is a Starlark module of math-related functions and constants. |
| 18 |
// The module defines the following functions: |
| 19 |
// |
| 20 |
// ceil(x) - Returns the ceiling of x, the smallest integer greater than or equal to x. |
| 21 |
// copysign(x, y) - Returns a value with the magnitude of x and the sign of y. |
| 22 |
// fabs(x) - Returns the absolute value of x as float. |
| 23 |
// floor(x) - Returns the floor of x, the largest integer less than or equal to x. |
| 24 |
// mod(x, y) - Returns the floating-point remainder of x/y. The magnitude of the result is less than y and its sign agrees with that of x. |
| 25 |
// pow(x, y) - Returns x**y, the base-x exponential of y. |
| 26 |
// remainder(x, y) - Returns the IEEE 754 floating-point remainder of x/y. |
| 27 |
// round(x) - Returns the nearest integer, rounding half away from zero. |
| 28 |
// |
| 29 |
// exp(x) - Returns e raised to the power x, where e = 2.718281… is the base of natural logarithms. |
| 30 |
// sqrt(x) - Returns the square root of x. |
| 31 |
// |
| 32 |
// acos(x) - Returns the arc cosine of x, in radians. |
| 33 |
// asin(x) - Returns the arc sine of x, in radians. |
| 34 |
// atan(x) - Returns the arc tangent of x, in radians. |
| 35 |
// atan2(y, x) - Returns atan(y / x), in radians. |
| 36 |
// The result is between -pi and pi. |
| 37 |
// The vector in the plane from the origin to point (x, y) makes this angle with the positive X axis. |
| 38 |
// The point of atan2() is that the signs of both inputs are known to it, so it can compute the correct |
| 39 |
// quadrant for the angle. |
| 40 |
// For example, atan(1) and atan2(1, 1) are both pi/4, but atan2(-1, -1) is -3*pi/4. |
| 41 |
// cos(x) - Returns the cosine of x, in radians. |
| 42 |
// hypot(x, y) - Returns the Euclidean norm, sqrt(x*x + y*y). This is the length of the vector from the origin to point (x, y). |
| 43 |
// sin(x) - Returns the sine of x, in radians. |
| 44 |
// tan(x) - Returns the tangent of x, in radians. |
| 45 |
// |
| 46 |
// degrees(x) - Converts angle x from radians to degrees. |
| 47 |
// radians(x) - Converts angle x from degrees to radians. |
| 48 |
// |
| 49 |
// acosh(x) - Returns the inverse hyperbolic cosine of x. |
| 50 |
// asinh(x) - Returns the inverse hyperbolic sine of x. |
| 51 |
// atanh(x) - Returns the inverse hyperbolic tangent of x. |
| 52 |
// cosh(x) - Returns the hyperbolic cosine of x. |
| 53 |
// sinh(x) - Returns the hyperbolic sine of x. |
| 54 |
// tanh(x) - Returns the hyperbolic tangent of x. |
| 55 |
// |
| 56 |
// log(x, base) - Returns the logarithm of x in the given base, or natural logarithm by default. |
| 57 |
// |
| 58 |
// gamma(x) - Returns the Gamma function of x. |
| 59 |
// |
| 60 |
// All functions accept both int and float values as arguments. |
| 61 |
// |
| 62 |
// The module also defines approximations of the following constants: |
| 63 |
// |
| 64 |
// e - The base of natural logarithms, approximately 2.71828. |
| 65 |
// pi - The ratio of a circle's circumference to its diameter, approximately 3.14159. |
| 66 |
var Module = &starlarkstruct.Module{ |
| 67 |
Name: "math", |
| 68 |
Members: starlark.StringDict{ |
| 69 |
"ceil": starlark.NewBuiltin("ceil", ceil), |
| 70 |
"copysign": newBinaryBuiltin("copysign", math.Copysign), |
| 71 |
"fabs": newUnaryBuiltin("fabs", math.Abs), |
| 72 |
"floor": starlark.NewBuiltin("floor", floor), |
| 73 |
"mod": newBinaryBuiltin("mod", math.Mod), |
| 74 |
"pow": newBinaryBuiltin("pow", math.Pow), |
| 75 |
"remainder": newBinaryBuiltin("remainder", math.Remainder), |
| 76 |
"round": newUnaryBuiltin("round", math.Round), |
| 77 |
|
| 78 |
"exp": newUnaryBuiltin("exp", math.Exp), |
| 79 |
"sqrt": newUnaryBuiltin("sqrt", math.Sqrt), |
| 80 |
|
| 81 |
"acos": newUnaryBuiltin("acos", math.Acos), |
| 82 |
"asin": newUnaryBuiltin("asin", math.Asin), |
| 83 |
"atan": newUnaryBuiltin("atan", math.Atan), |
| 84 |
"atan2": newBinaryBuiltin("atan2", math.Atan2), |
| 85 |
"cos": newUnaryBuiltin("cos", math.Cos), |
| 86 |
"hypot": newBinaryBuiltin("hypot", math.Hypot), |
| 87 |
"sin": newUnaryBuiltin("sin", math.Sin), |
| 88 |
"tan": newUnaryBuiltin("tan", math.Tan), |
| 89 |
|
| 90 |
"degrees": newUnaryBuiltin("degrees", degrees), |
| 91 |
"radians": newUnaryBuiltin("radians", radians), |
| 92 |
|
| 93 |
"acosh": newUnaryBuiltin("acosh", math.Acosh), |
| 94 |
"asinh": newUnaryBuiltin("asinh", math.Asinh), |
| 95 |
"atanh": newUnaryBuiltin("atanh", math.Atanh), |
| 96 |
"cosh": newUnaryBuiltin("cosh", math.Cosh), |
| 97 |
"sinh": newUnaryBuiltin("sinh", math.Sinh), |
| 98 |
"tanh": newUnaryBuiltin("tanh", math.Tanh), |
| 99 |
|
| 100 |
"log": starlark.NewBuiltin("log", log), |
| 101 |
|
| 102 |
"gamma": newUnaryBuiltin("gamma", math.Gamma), |
| 103 |
|
| 104 |
"e": starlark.Float(math.E), |
| 105 |
"pi": starlark.Float(math.Pi), |
| 106 |
}, |
| 107 |
} |
| 108 |
|
| 109 |
// floatOrInt is an Unpacker that converts a Starlark int or float to Go's float64. |
| 110 |
type floatOrInt float64 |
| 111 |
|
| 112 |
func (p *floatOrInt) Unpack(v starlark.Value) error { |
| 113 |
switch v := v.(type) { |
| 114 |
case starlark.Int: |
| 115 |
*p = floatOrInt(v.Float()) |
| 116 |
return nil |
| 117 |
case starlark.Float: |
| 118 |
*p = floatOrInt(v) |
| 119 |
return nil |
| 120 |
} |
| 121 |
return fmt.Errorf("got %s, want float or int", v.Type()) |
| 122 |
} |
| 123 |
|
| 124 |
// newUnaryBuiltin wraps a unary floating-point Go function |
| 125 |
// as a Starlark built-in that accepts int or float arguments. |
| 126 |
func newUnaryBuiltin(name string, fn func(float64) float64) *starlark.Builtin { |
| 127 |
return starlark.NewBuiltin(name, func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { |
| 128 |
var x floatOrInt |
| 129 |
if err := starlark.UnpackPositionalArgs(name, args, kwargs, 1, &x); err != nil { |
| 130 |
return nil, err |
| 131 |
} |
| 132 |
return starlark.Float(fn(float64(x))), nil |
| 133 |
}) |
| 134 |
} |
| 135 |
|
| 136 |
// newBinaryBuiltin wraps a binary floating-point Go function |
| 137 |
// as a Starlark built-in that accepts int or float arguments. |
| 138 |
func newBinaryBuiltin(name string, fn func(float64, float64) float64) *starlark.Builtin { |
| 139 |
return starlark.NewBuiltin(name, func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { |
| 140 |
var x, y floatOrInt |
| 141 |
if err := starlark.UnpackPositionalArgs(name, args, kwargs, 2, &x, &y); err != nil { |
| 142 |
return nil, err |
| 143 |
} |
| 144 |
return starlark.Float(fn(float64(x), float64(y))), nil |
| 145 |
}) |
| 146 |
} |
| 147 |
|
| 148 |
// log wraps the Log function |
| 149 |
// |
| 150 |
// as a Starlark built-in that accepts int or float arguments. |
| 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 |
| 163 |
} |
| 164 |
|
| 165 |
func ceil(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { |
| 166 |
var x starlark.Value |
| 167 |
|
| 168 |
if err := starlark.UnpackPositionalArgs("ceil", args, kwargs, 1, &x); err != nil { |
| 169 |
return nil, err |
| 170 |
} |
| 171 |
|
| 172 |
switch t := x.(type) { |
| 173 |
case starlark.Int: |
| 174 |
return t, nil |
| 175 |
case starlark.Float: |
| 176 |
return starlark.NumberToInt(starlark.Float(math.Ceil(float64(t)))) |
| 177 |
} |
| 178 |
|
| 179 |
return nil, fmt.Errorf("got %s, want float or int", x.Type()) |
| 180 |
} |
| 181 |
|
| 182 |
func floor(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { |
| 183 |
var x starlark.Value |
| 184 |
|
| 185 |
if err := starlark.UnpackPositionalArgs("floor", args, kwargs, 1, &x); err != nil { |
| 186 |
return nil, err |
| 187 |
} |
| 188 |
|
| 189 |
switch t := x.(type) { |
| 190 |
case starlark.Int: |
| 191 |
return t, nil |
| 192 |
case starlark.Float: |
| 193 |
return starlark.NumberToInt(starlark.Float(math.Floor(float64(t)))) |
| 194 |
} |
| 195 |
|
| 196 |
return nil, fmt.Errorf("got %s, want float or int", x.Type()) |
| 197 |
} |
| 198 |
|
| 199 |
func degrees(x float64) float64 { |
| 200 |
return 360 * x / (2 * math.Pi) |
| 201 |
} |
| 202 |
|
| 203 |
func radians(x float64) float64 { |
| 204 |
return 2 * math.Pi * x / 360 |
| 205 |
} |