-
Notifications
You must be signed in to change notification settings - Fork 228
add math module #357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add math module #357
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
be118f1
add math module
b5 f8a3101
math: Applied remarks
essobedo 00e681c
math: Applied remarks part 2
essobedo b19ffec
math: Applied remarks part 3
essobedo c1b7e05
math: Applied remarks part 4
essobedo a33965d
math: Add missing functions
essobedo 899dc83
math: Use a dedicated function for log
essobedo 3327e5d
time: Inline the log function
essobedo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| // Copyright 2021 The Bazel Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| // Package math provides basic constants and mathematical functions. | ||
| package math // import "go.starlark.net/lib/math" | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math" | ||
|
|
||
| "go.starlark.net/starlark" | ||
| "go.starlark.net/starlarkstruct" | ||
| ) | ||
|
|
||
| // Module math is a Starlark module of math-related functions and constants. | ||
| // The module defines the following functions: | ||
| // | ||
| // abs(x) - Returns the absolute value of x. | ||
| // ceil(x) - Returns the ceiling of x, the smallest integer greater than or equal to x. | ||
| // floor(x) - Returns the floor of x, the largest integer less than or equal to x. | ||
| // round(x) - Returns the nearest integer, rounding half away from zero. | ||
| // | ||
| // exp(x) - Returns e raised to the power x, where e = 2.718281… is the base of natural logarithms. | ||
| // sqrt(x) - Returns the square root of x. | ||
| // | ||
| // acos(x) - Returns the arc cosine of x, in radians. | ||
| // asin(x) - Returns the arc sine of x, in radians. | ||
| // atan(x) - Returns the arc tangent of x, in radians. | ||
| // atan2(y, x) - Returns atan(y / x), in radians. | ||
| // The result is between -pi and pi. | ||
| // The vector in the plane from the origin to point (x, y) makes this angle with the positive X axis. | ||
| // 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. | ||
| // For example, atan(1) and atan2(1, 1) are both pi/4, but atan2(-1, -1) is -3*pi/4. | ||
| // cos(x) - Returns the cosine of x, in radians. | ||
| // 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). | ||
| // sin(x) - Returns the sine of x, in radians. | ||
| // tan(x) - Returns the tangent of x, in radians. | ||
| // | ||
| // degrees(x) - Converts angle x from radians to degrees. | ||
| // radians(x) - Converts angle x from degrees to radians. | ||
| // | ||
| // acosh(x) - Returns the inverse hyperbolic cosine of x. | ||
| // asinh(x) - Returns the inverse hyperbolic sine of x. | ||
| // atanh(x) - Returns the inverse hyperbolic tangent of x. | ||
| // cosh(x) - Returns the hyperbolic cosine of x. | ||
| // sinh(x) - Returns the hyperbolic sine of x. | ||
| // tanh(x) - Returns the hyperbolic tangent of x. | ||
| // | ||
| // log(x) - Returns the natural logarithm of x. | ||
| // log2(x) - Returns the decimal logarithm of x. The special cases are the same as for Log. | ||
| // log10(x) - Returns the natural logarithm of 1 plus its argument x. It is more accurate than Log(1 + x) when x is near. | ||
| // | ||
| // All function accept both int and float values as arguments. | ||
adonovan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // | ||
| // The module also defines approximations of the following constants: | ||
| // | ||
| // e - The base of natural logarithms, approximately 2.71828. | ||
| // phi - The golden ratio, (1 + sqrt(5)) / 2, approximately 1.61803. | ||
adonovan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // pi - The ratio of a circle's circumference to its diameter, approximately 3.14159. | ||
| // | ||
| var Module = &starlarkstruct.Module{ | ||
| Name: "math", | ||
| Members: starlark.StringDict{ | ||
| "abs": newUnaryBuiltin("abs", math.Abs), | ||
| "ceil": newUnaryBuiltin("ceil", math.Ceil), | ||
| "floor": newUnaryBuiltin("floor", math.Floor), | ||
| "round": newUnaryBuiltin("round", math.Round), | ||
|
|
||
| "exp": newUnaryBuiltin("exp", math.Exp), | ||
| "sqrt": newUnaryBuiltin("sqrt", math.Sqrt), | ||
|
|
||
| "acos": newUnaryBuiltin("acos", math.Acos), | ||
| "asin": newUnaryBuiltin("asin", math.Asin), | ||
| "atan": newUnaryBuiltin("atan", math.Atan), | ||
| "atan2": newBinaryFunction("atan2", math.Atan2), | ||
| "cos": newUnaryBuiltin("cos", math.Cos), | ||
| "hypot": newBinaryFunction("hypot", math.Hypot), | ||
| "sin": newUnaryBuiltin("sin", math.Sin), | ||
| "tan": newUnaryBuiltin("tan", math.Tan), | ||
|
|
||
| "degrees": newUnaryBuiltin("degrees", degrees), | ||
| "radians": newUnaryBuiltin("radians", radians), | ||
|
|
||
| "acosh": newUnaryBuiltin("acosh", math.Acosh), | ||
| "asinh": newUnaryBuiltin("asinh", math.Asinh), | ||
| "atanh": newUnaryBuiltin("atanh", math.Atanh), | ||
| "cosh": newUnaryBuiltin("cosh", math.Cosh), | ||
| "sinh": newUnaryBuiltin("sinh", math.Sinh), | ||
| "tanh": newUnaryBuiltin("tanh", math.Tanh), | ||
|
|
||
| "log": newUnaryBuiltin("log", math.Log), | ||
adonovan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "log2": newUnaryBuiltin("log2", math.Log2), | ||
| "log10": newUnaryBuiltin("log10", math.Log10), | ||
|
|
||
| "e": starlark.Float(math.E), | ||
| "phi": starlark.Float(math.Phi), | ||
| "pi": starlark.Float(math.Pi), | ||
| }, | ||
| } | ||
|
|
||
| // floatOrInt is an Unpacker that converts a Starlark int or float to Go's float64. | ||
| type floatOrInt float64 | ||
|
|
||
| // Unpack is a custom argument unpacker | ||
adonovan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| func (p *floatOrInt) Unpack(v starlark.Value) error { | ||
| switch v := v.(type) { | ||
| case starlark.Int: | ||
| *p = floatOrInt(v.Float()) | ||
| return nil | ||
| case starlark.Float: | ||
| *p = floatOrInt(v) | ||
| return nil | ||
| } | ||
|
|
||
| return fmt.Errorf("got %s, want float or int", v.Type()) | ||
| } | ||
|
|
||
| // newUnaryBuiltin wraps a unary floating-point Go function | ||
| // as a Starlark built-in that accepts int or float arguments. | ||
| func newUnaryBuiltin(name string, fn func(float64) float64) *starlark.Builtin { | ||
| return starlark.NewBuiltin(name, func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
| var x floatOrInt | ||
| if err := starlark.UnpackPositionalArgs(name, args, kwargs, 1, &x); err != nil { | ||
| return nil, err | ||
| } | ||
| return starlark.Float(fn(float64(x))), nil | ||
| }) | ||
| } | ||
|
|
||
| // newBinaryFunction wraps a binary floating-point Go function | ||
| // as a Starlark built-in that accepts int or float arguments. | ||
| func newBinaryFunction(name string, fn func(float64, float64) float64) *starlark.Builtin { | ||
| return starlark.NewBuiltin(name, func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
| var x, y floatOrInt | ||
| if err := starlark.UnpackPositionalArgs(name, args, kwargs, 2, &x, &y); err != nil { | ||
| return nil, err | ||
| } | ||
| return starlark.Float(fn(float64(x), float64(y))), nil | ||
| }) | ||
| } | ||
|
|
||
| func degrees(x float64) float64 { | ||
| return 360 * x / (2 * math.Pi) | ||
| } | ||
|
|
||
| func radians(x float64) float64 { | ||
| return 2 * math.Pi * x / 360 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.