/
stdlib.go
112 lines (101 loc) · 2.78 KB
/
stdlib.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package script
import (
"fmt"
"math"
"math/rand"
"time"
)
// Loads standard functions into the world.
func (w *World) LoadStdlib() {
// literals
w.declare("true", boolLit(true))
w.declare("false", boolLit(false))
// math
w.Func("abs", math.Abs)
w.Func("acos", math.Acos)
w.Func("acosh", math.Acosh)
w.Func("asin", math.Asin)
w.Func("asinh", math.Asinh)
w.Func("atan", math.Atan)
w.Func("atanh", math.Atanh)
w.Func("cbrt", math.Cbrt)
w.Func("ceil", math.Ceil)
w.Func("cos", math.Cos)
w.Func("cosh", math.Cosh)
w.Func("erf", math.Erf)
w.Func("erfc", math.Erfc)
w.Func("exp", math.Exp)
w.Func("exp2", math.Exp2)
w.Func("expm1", math.Expm1)
w.Func("floor", math.Floor)
w.Func("gamma", math.Gamma)
w.Func("j0", math.J0)
w.Func("j1", math.J1)
w.Func("log", math.Log)
w.Func("log10", math.Log10)
w.Func("log1p", math.Log1p)
w.Func("log2", math.Log2)
w.Func("logb", math.Logb)
w.Func("sin", math.Sin)
w.Func("sinh", math.Sinh)
w.Func("sqrt", math.Sqrt)
w.Func("tan", math.Tan)
w.Func("tanh", math.Tanh)
w.Func("trunc", math.Trunc)
w.Func("y0", math.Y0)
w.Func("y1", math.Y1)
w.Func("ilogb", math.Ilogb)
w.Func("pow10", math.Pow10)
w.Func("atan2", math.Atan2)
w.Func("hypot", math.Hypot)
w.Func("remainder", math.Remainder)
w.Func("max", math.Max)
w.Func("min", math.Min)
w.Func("mod", math.Mod)
w.Func("pow", math.Pow)
w.Func("yn", math.Yn)
w.Func("jn", math.Jn)
w.Func("ldexp", math.Ldexp)
w.Func("isInf", math.IsInf)
w.Func("isNaN", math.IsNaN)
w.Func("norm", norm, "Standard normal distribution")
w.Func("heaviside", heaviside)
w.Func("sinc", sinc)
w.Func("randSeed", intseed, "Sets the random number seed")
w.Func("rand", rng.Float64, "Random number between 0 and 1")
w.Func("randExp", rng.ExpFloat64, "Exponentially distributed random number between 0 and +inf, mean=1")
w.Func("randNorm", rng.NormFloat64, "Standard normal random number")
w.Func("randInt", randInt, "Random non-negative integer")
w.declare("pi", floatLit(math.Pi))
w.declare("inf", floatLit(math.Inf(1)))
//string
w.Func("sprint", fmt.Sprint, "Print all arguments to string with automatic formatting")
w.Func("sprintf", fmt.Sprintf, "Print to string with C-style formatting.")
//time
w.Func("now", time.Now, "Returns the current time")
w.Func("since", time.Since, "Returns the time elapsed since argument")
}
var rng = rand.New(rand.NewSource(0))
// script does not know int64
func intseed(seed int) { rng.Seed(int64(seed)) }
func randInt(upper int) int { return rng.Int() % upper }
func heaviside(x float64) float64 {
switch {
default:
return 1
case x == 0:
return 0.5
case x < 0:
return 0
}
}
func norm(x float64) float64 {
return (1 / math.Sqrt(2*math.Pi)) * math.Exp(-0.5*x*x)
}
func sinc(x float64) float64 {
if x == 0 {
return 1
} else {
return math.Sin(x) / x
}
}