-
Notifications
You must be signed in to change notification settings - Fork 0
/
MantissaCalculator.js
41 lines (40 loc) · 1.04 KB
/
MantissaCalculator.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
class MantissaCalculator {
static nextId = 0;
constructor() {
this.div = document.createElement('div');
this.divdiv = document.createElement('div');
this.divdiv.id = "mantissacalculator#" + this.nextId++;
this.input = document.createElement('input');
this.div.append(
this.input,
this.divdiv
);
this.div.append(this.input);
document.body.appendChild(
//this.description,
this.div,
//this.output
);
this.input.oninput = this.oninput.bind(this);
}
oninput(e) {
var sum = 0;
var data = [];
var bitvalue;
this.input.value.split("").forEach((bit, pos)=>{
var add = 0;
if (bit == '1') {
bitvalue = 1 << (pos+1);
add = 1 / bitvalue;
sum += add;
}
data.push([bit, pos, bitvalue, add, sum]);
});
tabulate(data, {
id: this.divdiv.id,
arrive: '<h2>Mantissa Calculator</h2>',
finalize: `<b><center>Final value: ${sum}</center></b>`,
header: ['bit', 'pos', 'bitvalue', 'add', 'sum']
});
}
}