Skip to content

Commit

Permalink
Added more math consts (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
vereecw authored Jan 30, 2023
1 parent 3f4245d commit ecc3d91
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
22 changes: 22 additions & 0 deletions docs/stdlib-math.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ math := import("math")
- `ln10`
- `ln10E`

Mathematical constants.

- `maxFloat32`
- `smallestNonzeroFloat32`
- `maxFloat64`
- `smallestNonzeroFloat64`

Floating-point limit values. Max is the largest finite value representable by the type. SmallestNonzero is the smallest positive, non-zero value representable by the type.

- `maxInt`
- `minInt`
- `maxInt8`
- `minInt8`
- `maxInt16`
- `minInt16`
- `maxInt32`
- `minInt32`
- `maxInt64`
- `minInt64`

Integer limit values.

## Functions

- `abs(x float) => float`: returns the absolute value of x.
Expand Down
36 changes: 25 additions & 11 deletions stdlib/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,31 @@ import (
)

var mathModule = map[string]tengo.Object{
"e": &tengo.Float{Value: math.E},
"pi": &tengo.Float{Value: math.Pi},
"phi": &tengo.Float{Value: math.Phi},
"sqrt2": &tengo.Float{Value: math.Sqrt2},
"sqrtE": &tengo.Float{Value: math.SqrtE},
"sqrtPi": &tengo.Float{Value: math.SqrtPi},
"sqrtPhi": &tengo.Float{Value: math.SqrtPhi},
"ln2": &tengo.Float{Value: math.Ln2},
"log2E": &tengo.Float{Value: math.Log2E},
"ln10": &tengo.Float{Value: math.Ln10},
"log10E": &tengo.Float{Value: math.Log10E},
"e": &tengo.Float{Value: math.E},
"pi": &tengo.Float{Value: math.Pi},
"phi": &tengo.Float{Value: math.Phi},
"sqrt2": &tengo.Float{Value: math.Sqrt2},
"sqrtE": &tengo.Float{Value: math.SqrtE},
"sqrtPi": &tengo.Float{Value: math.SqrtPi},
"sqrtPhi": &tengo.Float{Value: math.SqrtPhi},
"ln2": &tengo.Float{Value: math.Ln2},
"log2E": &tengo.Float{Value: math.Log2E},
"ln10": &tengo.Float{Value: math.Ln10},
"log10E": &tengo.Float{Value: math.Log10E},
"maxFloat32": &tengo.Float{Value: math.MaxFloat32},
"smallestNonzeroFloat32": &tengo.Float{Value: math.SmallestNonzeroFloat32},
"maxFloat64": &tengo.Float{Value: math.MaxFloat64},
"smallestNonzeroFloat64": &tengo.Float{Value: math.SmallestNonzeroFloat64},
"maxInt": &tengo.Int{Value: math.MaxInt},
"minInt": &tengo.Int{Value: math.MinInt},
"maxInt8": &tengo.Int{Value: math.MaxInt8},
"minInt8": &tengo.Int{Value: math.MinInt8},
"maxInt16": &tengo.Int{Value: math.MaxInt16},
"minInt16": &tengo.Int{Value: math.MinInt16},
"maxInt32": &tengo.Int{Value: math.MaxInt32},
"minInt32": &tengo.Int{Value: math.MinInt32},
"maxInt64": &tengo.Int{Value: math.MaxInt64},
"minInt64": &tengo.Int{Value: math.MinInt64},
"abs": &tengo.UserFunction{
Name: "abs",
Value: FuncAFRF(math.Abs),
Expand Down

2 comments on commit ecc3d91

@liufuqiang
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not recommended. Because it will make the lower version of golang (such as go 1.16.x) unusable
Such as: math.MaxInt, math.MinInt was introduced in a later version。

@geseq
Copy link
Collaborator

@geseq geseq commented on ecc3d91 Jan 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We aim to support 2-3 versions before current Go version. Given go1.20 isn’t that far away, I think we can wait until that point before releasing this.

Please sign in to comment.