forked from gopherjs/gopherjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.go
227 lines (182 loc) · 4.02 KB
/
math.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// +build js
package math
import (
"github.com/gopherjs/gopherjs/js"
)
var math = js.Global.Get("Math")
var zero float64 = 0
var posInf = 1 / zero
var negInf = -1 / zero
var nan = 0 / zero
func Abs(x float64) float64 {
return abs(x)
}
func Acos(x float64) float64 {
return math.Call("acos", x).Float()
}
func Asin(x float64) float64 {
return math.Call("asin", x).Float()
}
func Atan(x float64) float64 {
return math.Call("atan", x).Float()
}
func Atan2(y, x float64) float64 {
return math.Call("atan2", y, x).Float()
}
func Ceil(x float64) float64 {
return math.Call("ceil", x).Float()
}
func Copysign(x, y float64) float64 {
if (x < 0 || 1/x == negInf) != (y < 0 || 1/y == negInf) {
return -x
}
return x
}
func Cos(x float64) float64 {
return math.Call("cos", x).Float()
}
func Dim(x, y float64) float64 {
return dim(x, y)
}
func Exp(x float64) float64 {
return math.Call("exp", x).Float()
}
func Exp2(x float64) float64 {
return math.Call("pow", 2, x).Float()
}
func Expm1(x float64) float64 {
return expm1(x)
}
func Floor(x float64) float64 {
return math.Call("floor", x).Float()
}
func Frexp(f float64) (frac float64, exp int) {
return frexp(f)
}
func Hypot(p, q float64) float64 {
return hypot(p, q)
}
func Inf(sign int) float64 {
switch {
case sign >= 0:
return posInf
default:
return negInf
}
}
func IsInf(f float64, sign int) bool {
if f == posInf {
return sign >= 0
}
if f == negInf {
return sign <= 0
}
return false
}
func IsNaN(f float64) (is bool) {
return f != f
}
func Ldexp(frac float64, exp int) float64 {
if frac == 0 {
return frac
}
if exp >= 1024 {
return frac * math.Call("pow", 2, 1023).Float() * math.Call("pow", 2, exp-1023).Float()
}
if exp <= -1024 {
return frac * math.Call("pow", 2, -1023).Float() * math.Call("pow", 2, exp+1023).Float()
}
return frac * math.Call("pow", 2, exp).Float()
}
func Log(x float64) float64 {
if x != x { // workaround for optimizer bug in V8, remove at some point
return nan
}
return math.Call("log", x).Float()
}
func Log10(x float64) float64 {
return log10(x)
}
func Log1p(x float64) float64 {
return log1p(x)
}
func Log2(x float64) float64 {
return log2(x)
}
func Max(x, y float64) float64 {
return max(x, y)
}
func Min(x, y float64) float64 {
return min(x, y)
}
func Mod(x, y float64) float64 {
return js.Global.Call("$mod", x, y).Float()
}
func Modf(f float64) (float64, float64) {
if f == posInf || f == negInf {
return f, nan
}
frac := Mod(f, 1)
return f - frac, frac
}
func NaN() float64 {
return nan
}
func Pow(x, y float64) float64 {
if x == 1 || (x == -1 && (y == posInf || y == negInf)) {
return 1
}
return math.Call("pow", x, y).Float()
}
func Remainder(x, y float64) float64 {
return remainder(x, y)
}
func Signbit(x float64) bool {
return x < 0 || 1/x == negInf
}
func Sin(x float64) float64 {
return math.Call("sin", x).Float()
}
func Sincos(x float64) (sin, cos float64) {
return Sin(x), Cos(x)
}
func Sqrt(x float64) float64 {
return math.Call("sqrt", x).Float()
}
func Tan(x float64) float64 {
return math.Call("tan", x).Float()
}
func Trunc(x float64) float64 {
if x == posInf || x == negInf || x != x || 1/x == negInf {
return x
}
return float64(int(x))
}
var buf struct {
uint32array [2]uint32
float32array [2]float32
float64array [1]float64
}
func init() {
ab := js.Global.Get("ArrayBuffer").New(8)
js.InternalObject(buf).Set("uint32array", js.Global.Get("Uint32Array").New(ab))
js.InternalObject(buf).Set("float32array", js.Global.Get("Float32Array").New(ab))
js.InternalObject(buf).Set("float64array", js.Global.Get("Float64Array").New(ab))
}
func Float32bits(f float32) uint32 {
buf.float32array[0] = f
return buf.uint32array[0]
}
func Float32frombits(b uint32) float32 {
buf.uint32array[0] = b
return buf.float32array[0]
}
func Float64bits(f float64) uint64 {
buf.float64array[0] = f
return uint64(buf.uint32array[1])<<32 + uint64(buf.uint32array[0])
}
func Float64frombits(b uint64) float64 {
buf.uint32array[0] = uint32(b)
buf.uint32array[1] = uint32(b >> 32)
return buf.float64array[0]
}