| 1 |
// +build js |
| 2 |
|
| 3 |
package math |
| 4 |
|
| 5 |
import ( |
| 6 |
"github.com/gopherjs/gopherjs/js" |
| 7 |
) |
| 8 |
|
| 9 |
var math = js.Global.Get("Math") |
| 10 |
var zero float64 = 0 |
| 11 |
var posInf = 1 / zero |
| 12 |
var negInf = -1 / zero |
| 13 |
var nan = 0 / zero |
| 14 |
|
| 15 |
func Acos(x float64) float64 { |
| 16 |
return math.Call("acos", x).Float() |
| 17 |
} |
| 18 |
|
| 19 |
func Acosh(x float64) float64 { |
| 20 |
return math.Call("acosh", x).Float() |
| 21 |
} |
| 22 |
|
| 23 |
func Asin(x float64) float64 { |
| 24 |
return math.Call("asin", x).Float() |
| 25 |
} |
| 26 |
|
| 27 |
func Asinh(x float64) float64 { |
| 28 |
return math.Call("asinh", x).Float() |
| 29 |
} |
| 30 |
|
| 31 |
func Atan(x float64) float64 { |
| 32 |
return math.Call("atan", x).Float() |
| 33 |
} |
| 34 |
|
| 35 |
func Atanh(x float64) float64 { |
| 36 |
return math.Call("atanh", x).Float() |
| 37 |
} |
| 38 |
|
| 39 |
func Atan2(y, x float64) float64 { |
| 40 |
return math.Call("atan2", y, x).Float() |
| 41 |
} |
| 42 |
|
| 43 |
func Cbrt(x float64) float64 { |
| 44 |
return math.Call("cbrt", x).Float() |
| 45 |
} |
| 46 |
|
| 47 |
func Ceil(x float64) float64 { |
| 48 |
return math.Call("ceil", x).Float() |
| 49 |
} |
| 50 |
|
| 51 |
func Copysign(x, y float64) float64 { |
| 52 |
if (x < 0 || 1/x == negInf) != (y < 0 || 1/y == negInf) { |
| 53 |
return -x |
| 54 |
} |
| 55 |
return x |
| 56 |
} |
| 57 |
|
| 58 |
func Cos(x float64) float64 { |
| 59 |
return math.Call("cos", x).Float() |
| 60 |
} |
| 61 |
|
| 62 |
func Cosh(x float64) float64 { |
| 63 |
return math.Call("cosh", x).Float() |
| 64 |
} |
| 65 |
|
| 66 |
func Dim(x, y float64) float64 { |
| 67 |
return dim(x, y) |
| 68 |
} |
| 69 |
|
| 70 |
func Erf(x float64) float64 { |
| 71 |
return erf(x) |
| 72 |
} |
| 73 |
|
| 74 |
func Erfc(x float64) float64 { |
| 75 |
return erfc(x) |
| 76 |
} |
| 77 |
|
| 78 |
func Exp(x float64) float64 { |
| 79 |
return math.Call("exp", x).Float() |
| 80 |
} |
| 81 |
|
| 82 |
func Exp2(x float64) float64 { |
| 83 |
return math.Call("pow", 2, x).Float() |
| 84 |
} |
| 85 |
|
| 86 |
func Expm1(x float64) float64 { |
| 87 |
return expm1(x) |
| 88 |
} |
| 89 |
|
| 90 |
func Floor(x float64) float64 { |
| 91 |
return math.Call("floor", x).Float() |
| 92 |
} |
| 93 |
|
| 94 |
func Frexp(f float64) (frac float64, exp int) { |
| 95 |
return frexp(f) |
| 96 |
} |
| 97 |
|
| 98 |
func Hypot(p, q float64) float64 { |
| 99 |
return hypot(p, q) |
| 100 |
} |
| 101 |
|
| 102 |
func Inf(sign int) float64 { |
| 103 |
switch { |
| 104 |
case sign >= 0: |
| 105 |
return posInf |
| 106 |
default: |
| 107 |
return negInf |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func IsInf(f float64, sign int) bool { |
| 112 |
if f == posInf { |
| 113 |
return sign >= 0 |
| 114 |
} |
| 115 |
if f == negInf { |
| 116 |
return sign <= 0 |
| 117 |
} |
| 118 |
return false |
| 119 |
} |
| 120 |
|
| 121 |
func IsNaN(f float64) (is bool) { |
| 122 |
return f != f |
| 123 |
} |
| 124 |
|
| 125 |
func Ldexp(frac float64, exp int) float64 { |
| 126 |
if frac == 0 { |
| 127 |
return frac |
| 128 |
} |
| 129 |
if exp >= 1024 { |
| 130 |
return frac * math.Call("pow", 2, 1023).Float() * math.Call("pow", 2, exp-1023).Float() |
| 131 |
} |
| 132 |
if exp <= -1024 { |
| 133 |
return frac * math.Call("pow", 2, -1023).Float() * math.Call("pow", 2, exp+1023).Float() |
| 134 |
} |
| 135 |
return frac * math.Call("pow", 2, exp).Float() |
| 136 |
} |
| 137 |
|
| 138 |
func Log(x float64) float64 { |
| 139 |
if x != x { // workaround for optimizer bug in V8, remove at some point |
| 140 |
return nan |
| 141 |
} |
| 142 |
return math.Call("log", x).Float() |
| 143 |
} |
| 144 |
|
| 145 |
func Log10(x float64) float64 { |
| 146 |
return log10(x) |
| 147 |
} |
| 148 |
|
| 149 |
func Log1p(x float64) float64 { |
| 150 |
return log1p(x) |
| 151 |
} |
| 152 |
|
| 153 |
func Log2(x float64) float64 { |
| 154 |
return log2(x) |
| 155 |
} |
| 156 |
|
| 157 |
func Max(x, y float64) float64 { |
| 158 |
return max(x, y) |
| 159 |
} |
| 160 |
|
| 161 |
func Min(x, y float64) float64 { |
| 162 |
return min(x, y) |
| 163 |
} |
| 164 |
|
| 165 |
func Mod(x, y float64) float64 { |
| 166 |
return js.Global.Call("$mod", x, y).Float() |
| 167 |
} |
| 168 |
|
| 169 |
func Modf(f float64) (float64, float64) { |
| 170 |
if f == posInf || f == negInf { |
| 171 |
return f, nan |
| 172 |
} |
| 173 |
if 1/f == negInf { |
| 174 |
return f, f |
| 175 |
} |
| 176 |
frac := Mod(f, 1) |
| 177 |
return f - frac, frac |
| 178 |
} |
| 179 |
|
| 180 |
func NaN() float64 { |
| 181 |
return nan |
| 182 |
} |
| 183 |
|
| 184 |
func Pow(x, y float64) float64 { |
| 185 |
if x == 1 || (x == -1 && (y == posInf || y == negInf)) { |
| 186 |
return 1 |
| 187 |
} |
| 188 |
return math.Call("pow", x, y).Float() |
| 189 |
} |
| 190 |
|
| 191 |
func Remainder(x, y float64) float64 { |
| 192 |
return remainder(x, y) |
| 193 |
} |
| 194 |
|
| 195 |
func Signbit(x float64) bool { |
| 196 |
return x < 0 || 1/x == negInf |
| 197 |
} |
| 198 |
|
| 199 |
func Sin(x float64) float64 { |
| 200 |
return math.Call("sin", x).Float() |
| 201 |
} |
| 202 |
|
| 203 |
func Sinh(x float64) float64 { |
| 204 |
return math.Call("sinh", x).Float() |
| 205 |
} |
| 206 |
|
| 207 |
func Sincos(x float64) (sin, cos float64) { |
| 208 |
return Sin(x), Cos(x) |
| 209 |
} |
| 210 |
|
| 211 |
func Sqrt(x float64) float64 { |
| 212 |
return math.Call("sqrt", x).Float() |
| 213 |
} |
| 214 |
|
| 215 |
func Tan(x float64) float64 { |
| 216 |
return math.Call("tan", x).Float() |
| 217 |
} |
| 218 |
|
| 219 |
func Tanh(x float64) float64 { |
| 220 |
return math.Call("tanh", x).Float() |
| 221 |
} |
| 222 |
|
| 223 |
func Trunc(x float64) float64 { |
| 224 |
if x == posInf || x == negInf || x != x || 1/x == negInf { |
| 225 |
return x |
| 226 |
} |
| 227 |
return float64(int(x)) |
| 228 |
} |
| 229 |
|
| 230 |
var buf struct { |
| 231 |
uint32array [2]uint32 |
| 232 |
float32array [2]float32 |
| 233 |
float64array [1]float64 |
| 234 |
} |
| 235 |
|
| 236 |
func init() { |
| 237 |
ab := js.Global.Get("ArrayBuffer").New(8) |
| 238 |
js.InternalObject(buf).Set("uint32array", js.Global.Get("Uint32Array").New(ab)) |
| 239 |
js.InternalObject(buf).Set("float32array", js.Global.Get("Float32Array").New(ab)) |
| 240 |
js.InternalObject(buf).Set("float64array", js.Global.Get("Float64Array").New(ab)) |
| 241 |
} |
| 242 |
|
| 243 |
func Float32bits(f float32) uint32 { |
| 244 |
buf.float32array[0] = f |
| 245 |
return buf.uint32array[0] |
| 246 |
} |
| 247 |
|
| 248 |
func Float32frombits(b uint32) float32 { |
| 249 |
buf.uint32array[0] = b |
| 250 |
return buf.float32array[0] |
| 251 |
} |
| 252 |
|
| 253 |
func Float64bits(f float64) uint64 { |
| 254 |
buf.float64array[0] = f |
| 255 |
return uint64(buf.uint32array[1])<<32 + uint64(buf.uint32array[0]) |
| 256 |
} |
| 257 |
|
| 258 |
func Float64frombits(b uint64) float64 { |
| 259 |
buf.uint32array[0] = uint32(b) |
| 260 |
buf.uint32array[1] = uint32(b >> 32) |
| 261 |
return buf.float64array[0] |
| 262 |
} |