forked from evanw/float-toy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
454 lines (399 loc) · 12.7 KB
/
index.html
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<meta charset="utf8">
<title>Float Toy</title>
<style>
body {
margin: 50px;
font: 14px/20px sans-serif;
cursor: default;
}
a {
color: inherit;
}
p {
margin: 30px 0;
}
h2 {
margin: 60px 0 30px 0;
}
table {
border-collapse: collapse;
font-size: 20px;
line-height: 14px;
width: 100%;
}
td {
font: inherit;
padding: 0;
text-align: center;
}
td.sign { background: #BBF; }
td.exponent { background: #BFB; }
td.fraction { background: #FBB; }
td.sign.hover { background: #99D; }
td.exponent.hover { background: #9D9; }
td.fraction.hover { background: #D99; }
.input td {
border: 1px solid #000;
padding: 5px 2px;
font-size: 14px;
}
.zerox-col {
vertical-align: bottom;
}
.zerox {
height: 26px;
line-height: 26px;
}
.output-col {
width: 100%;
vertical-align: bottom;
}
.hex-col {
width: 100%;
}
.output {
font: inherit;
padding: 5px;
margin: -5px;
border: 0;
outline: none;
width: 100%;
min-width: 200px;
background: none;
height: 26px;
line-height: 26px;
box-sizing: content-box;
}
.output:hover, .output:focus {
background: rgba(0, 0, 0, 0.066);
}
.help {
height: 50px;
font-size: 20px;
line-height: 60px;
padding-left: 3px;
white-space: nowrap;
}
.help sup {
vertical-align: top;
font-size: 12px;
line-height: 50px;
}
.help span {
padding: 3px;
margin: -3px;
}
td.nibble {
font-size: 10px;
line-height: 6px;
border: none;
}
td.dark.nibble {
background: #DDD;
}
span.sign { background: #DDF; }
span.exponent { background: #DFD; }
span.fraction { background: #FDD; }
</style>
<body>
<h1>Float Toy</h1>
<p>
Click on a cell below to toggle bit values, or edit the hex or decimal values directly.
Use this to build intuition for the IEEE floating-point format.
See Wikipedia for details on the <a href="https://en.wikipedia.org/wiki/Half-precision_floating-point_format">half-precision</a>, <a href="https://en.wikipedia.org/wiki/Single-precision_floating-point_format">single-precision</a> and <a href="https://en.wikipedia.org/wiki/Double-precision_floating-point_format">double-precision</a> floating-point formats.
</p>
<h2>16-bit (half)</h2>
<table>
<tr>
<td><span id="input16" class="input"></span></td>
<td class="zerox-col"><span class="zerox"> = 0x</span></td>
<td class="output-col"><input id="hex16" class="output"></td>
</tr>
</table>
<table>
<tr>
<td><span id="help16" class="help"></span></td>
<td> = </td>
<td class="hex-col"><input id="output16" class="output"></td>
</tr>
</table>
<h2>32-bit (float)</h2>
<table>
<tr>
<td><span id="input32" class="input"></span></td>
<td class="zerox-col"><span class="zerox"> = 0x</span></td>
<td class="output-col"><input id="hex32" class="output"></td>
</tr>
</table>
<table>
<tr>
<td><span id="help32" class="help"></span></td>
<td> = </td>
<td class="hex-col"><input id="output32" class="output"></td>
</tr>
</table>
<h2>64-bit (double)</h2>
<table>
<tr>
<td><span id="input64" class="input"></span></td>
<td class="zerox-col"><span class="zerox"> = 0x</span></td>
<td class="output-col"><input id="hex64" class="output"></td>
</tr>
</table>
<table>
<tr>
<td><span id="help64" class="help"></span></td>
<td> = </td>
<td class="hex-col"><input id="output64" class="output"></td>
</tr>
</table>
</body>
<script src="./float16.js"></script>
<script>
const {Float16Array} = float16;
(function () {
function load(array, exponentBits, input, output, help, hex) {
var bytes = new Uint8Array(array.buffer);
var hexLength = bytes.length;
function reduceNumber(x) {
var copy = bytes.length === 2 ? new Float16Array(1) : bytes.length === 4 ? new Float32Array(1) : new Float64Array(1);
copy[0] = +x;
var value = copy[0];
x = value === 0 && 1 / value === -Infinity ? '-0' : value + '';
if (x === 'NaN' || x === 'Infinity' || x === '-Infinity') {
return x;
}
var parts = /^([+-]?\d+)((?:\.\d+)?)((?:[eE][+-]?\d+)?)$/.exec(x);
var whole = parts[1];
var fraction = parts[2];
var exponent = parts[3];
// Remove digits one-by-one until the number changes
while (fraction.length > 0) {
// Try truncating
var truncatedFraction = fraction.slice(0, -1);
var text = whole + (truncatedFraction !== '.' ? truncatedFraction : '') + exponent;
copy[0] = +text;
var truncatedValue = copy[0];
if (truncatedValue === value) {
fraction = truncatedFraction;
x = text;
continue;
}
// Try rounding
var roundedFraction = truncatedFraction;
var i = roundedFraction.length - 1;
var zero = '0'.charCodeAt(0);
while (i > 0) {
var c = roundedFraction.charCodeAt(i) - zero;
roundedFraction = roundedFraction.slice(0, i) + String.fromCharCode((c + 1) % 10 + zero) + roundedFraction.slice(i + 1);
if (c < 9) break; // Do we need to carry?
i--;
}
var text = whole + (roundedFraction !== '.' ? roundedFraction : '') + exponent;
copy[0] = +text;
var roundedValue = copy[0];
if (roundedValue === value) {
fraction = roundedFraction;
x = text;
continue;
}
// Both numbers changed, keep the old value
break;
}
return x;
}
// Populate HTML
var html = '<table>';
// The bit numbers
html += '<tr>';
for (var i = 0; i < bytes.length; i++) {
for (var j = 0; j < 8; j++) {
var index = (bytes.length - i) * 8 - j;
if (j > 3) {
html += '<td class="nibble">' + index + '</td>'
} else {
html += '<td class="dark nibble">' + index + '</td>'
}
}
}
html += '</tr>';
// The bits
html += '<tr>';
for (var i = 0; i < bytes.length; i++) {
for (var j = 0; j < 8; j++) {
var index = i * 8 + j;
var className =
index === 0 ? 'sign' :
index < 1 + exponentBits ? 'exponent' :
'fraction';
html += '<td data-index="' + index + '" class="' + className + '">0</td>';
}
}
html += '</tr></table>';
input.innerHTML = html;
// Grab elements
var elements = [];
for (var i = 0; i < bytes.length * 8; i++) {
(function (i) {
var element = input.querySelector('[data-index="' + i + '"]');
element.onmouseover = function () { this.classList.add('hover'); };
element.onmouseout = function () { this.classList.remove('hover'); };
elements.push(element);
})(i);
}
// Event handlers
function extractNumber() {
return +(output.value.replace(/\b(?:infinity|inf)\b/gi, 'Infinity'));
}
output.onkeydown = function (e) {
if (e.which === 13) {
e.preventDefault();
output.blur();
}
else if (e.which === 38) {
e.preventDefault();
output.value = reduceNumber(extractNumber() + 1);
output.select();
output.oninput();
}
else if (e.which === 40) {
e.preventDefault();
output.value = reduceNumber(extractNumber() - 1);
output.select();
output.oninput();
}
};
output.onfocus = function () {
output.select();
};
output.oninput = function () {
array[0] = extractNumber();
render();
};
output.onblur = function () {
render();
};
hex.onkeydown = function (e) {
if (e.which === 13) {
e.preventDefault();
hex.blur();
}
};
hex.onfocus = function () {
hex.select();
};
hex.oninput = function () {
var hexAlphabet = '0123456789abcdefABCDEF';
var validHexCharas = hex.value.split('').every(function (c) {
return hexAlphabet.split('').lastIndexOf(c) !== -1;
});
if (hex.value.length > (hexLength * 2) || validHexCharas === false) {
hex.value = hex.value.slice(0, -1);
return;
}
var tmpBytes = toByteArray(hex.value);
bytes.fill(0);
bytes.set(tmpBytes.reverse(), hexLength - tmpBytes.length);
render();
};
hex.onblur = function () {
render();
};
input.onmousedown = function (e) {
if ('index' in e.target.dataset) {
var index = e.target.dataset.index | 0;
var byteIndex = bytes.length - (index >> 3) - 1;
var byteMask = 1 << (7 - (index & 7));
var mouseDownValue = bytes[byteIndex] & byteMask ? 0 : 1;
bytes[byteIndex] ^= byteMask;
render();
document.onmousemove = function (e2) {
if ('index' in e2.target.dataset) {
var index = e2.target.dataset.index | 0;
var byteIndex = bytes.length - (index >> 3) - 1;
var byteMask = 1 << (7 - (index & 7));
bytes[byteIndex] = (bytes[byteIndex] & ~byteMask) | (byteMask * mouseDownValue);
render();
}
};
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
};
}
};
// Update loop
function render() {
for (var i = 0; i < bytes.length * 8; i++) {
elements[i].textContent = ((bytes[bytes.length - (i >> 3) - 1] >> (7 - (i & 7))) & 1);
}
// Figure out exponent
var exponent = 0;
for (var i = 0; i < exponentBits; i++) {
var index = 1 + i;
var bit = (bytes[bytes.length - (index >> 3) - 1] >> (7 - (index & 7))) & 1;
exponent += bit << (exponentBits - i - 1);
}
var exponentBias = (1 << (exponentBits - 1)) - 1;
exponent -= exponentBias;
// Figure out fraction
var copyBytes = new Uint8Array(bytes);
var copy = bytes.length === 2 ? new Float16Array(copyBytes.buffer) : bytes.length === 4 ? new Float32Array(copyBytes.buffer) : new Float64Array(copyBytes.buffer);
for (var i = 0; i < exponentBits; i++) {
var index = 1 + i;
var byteIndex = bytes.length - (index >> 3) - 1;
var byteMask = 1 << (7 - (index & 7));
copyBytes[byteIndex] = (copyBytes[byteIndex] & ~byteMask) | (i === 0 ? 0 : byteMask);
}
var signIndex = bytes.length - 1;
var signMask = 0x80;
var sign = copyBytes[signIndex] & signMask;
copyBytes[signIndex] &= ~signMask;
var fraction = copy[0];
// Handle denormal numbers
if (exponent === -exponentBias) {
exponent++;
fraction--;
}
// Update views according to which input was edited
if (document.activeElement === hex) {
var value = array[0];
output.value = reduceNumber(value === 0 && 1 / value === -Infinity ? '-0' : value);
} else if (document.activeElement === output) {
var tmpBytes = bytes.slice().reverse();
hex.value = toHexString(tmpBytes);
} else { // This branch is for when the individual bits get toggled
var value = array[0];
output.value = reduceNumber(value === 0 && 1 / value === -Infinity ? '-0' : value);
var tmpBytes = bytes.slice().reverse();
hex.value = toHexString(tmpBytes);
}
help.innerHTML =
'<span class="sign">' + (sign ? -1 : 1) + '</span>' +
' × ' +
'<span class="exponent">2<sup>' + exponent + '</sup></span>' +
' × ' +
'<span class="fraction">' + reduceNumber(fraction) + '</span>';
}
function toHexString(byteArray) {
return Array.from(byteArray, function (byte) {
return ('0' + byte.toString(16).toUpperCase()).slice(-2);
}).join('')
}
function toByteArray(hexString) {
var result = [];
if (hexString.length % 2 == 1) {
hexString = hexString + '0';
}
for (var i = 0; i < hexString.length; i += 2) {
result.push(parseInt(hexString.substr(i, 2), 16));
}
return result;
}
render();
}
load(new Float16Array([Math.PI]), 5, document.getElementById('input16'), document.getElementById('output16'), document.getElementById('help16'), document.getElementById('hex16'));
load(new Float32Array([Math.PI]), 8, document.getElementById('input32'), document.getElementById('output32'), document.getElementById('help32'), document.getElementById('hex32'));
load(new Float64Array([Math.PI]), 11, document.getElementById('input64'), document.getElementById('output64'), document.getElementById('help64'), document.getElementById('hex64'));
})();
</script>