forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaml_float.js
129 lines (117 loc) · 2.03 KB
/
caml_float.js
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
let int_float_of_bits = (function(x){
return new Float32Array(new Int32Array([x]).buffer)[0]
});
let int_bits_of_float = (function(x){
return new Int32Array(new Float32Array([x]).buffer)[0]
});
function modf_float(x) {
if (!isFinite(x)) {
if (isNaN(x)) {
return [
NaN,
NaN
];
} else {
return [
1 / x,
x
];
}
}
let neg = 1 / x < 0;
let x$1 = Math.abs(x);
let i = Math.floor(x$1);
let f = x$1 - i;
if (neg) {
return [
- f,
- i
];
} else {
return [
f,
i
];
}
}
function ldexp_float(x, exp) {
let x$p = x;
let exp$p = exp;
if (exp$p > 1023) {
exp$p = exp$p - 1023;
x$p = x$p * Math.pow(2, 1023);
if (exp$p > 1023) {
exp$p = exp$p - 1023;
x$p = x$p * Math.pow(2, 1023);
}
} else if (exp$p < -1023) {
exp$p = exp$p + 1023;
x$p = x$p * Math.pow(2, -1023);
}
return x$p * Math.pow(2, exp$p);
}
function frexp_float(x) {
if (x === 0 || !isFinite(x)) {
return [
x,
0
];
}
let neg = x < 0;
let x$p = Math.abs(x);
let exp = Math.floor(Math.LOG2E * Math.log(x$p)) + 1;
x$p = x$p * Math.pow(2, - exp);
if (x$p < 0.5) {
x$p = x$p * 2;
exp = exp - 1;
}
if (neg) {
x$p = - x$p;
}
return [
x$p,
exp | 0
];
}
function copysign_float(x, y) {
let x$1 = Math.abs(x);
let y$1 = y === 0 ? 1 / y : y;
if (y$1 < 0) {
return - x$1;
} else {
return x$1;
}
}
function expm1_float(x) {
let y = Math.exp(x);
let z = y - 1;
if (Math.abs(x) > 1) {
return z;
} else if (z === 0) {
return x;
} else {
return x * z / Math.log(y);
}
}
function hypot_float(x, y) {
let x0 = Math.abs(x);
let y0 = Math.abs(y);
let a = x0 > y0 ? x0 : y0;
let b = (
x0 < y0 ? x0 : y0
) / (
a !== 0 ? a : 1
);
return a * Math.sqrt(1 + b * b);
}
export {
int_float_of_bits,
int_bits_of_float,
modf_float,
ldexp_float,
frexp_float,
copysign_float,
expm1_float,
hypot_float,
}
/* No side effect */