-
Notifications
You must be signed in to change notification settings - Fork 0
/
ieee754.html
76 lines (73 loc) · 3.48 KB
/
ieee754.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
<html>
<head>
<title>Test with Typed Arrays</title>
<script type="text/javascript" src="ieee754.js">
</script>
</head>
<body>
<script type="text/javascript">
s = 'Test with Typed Arrays<br/><br/>';
// System byte order:
s += 'System byte order is: ' + ieee754.endianness() + '<br/><br/>';
// Set work arrays
values = ['26', '-26', '26.4', '-26.4', 'n26', 'n-26'];
bitCounts = [8,16,32,64];
isLittleEndians = [true, false];
isIntegers = [true, false];
isSigneds = [true, false];
// Set loops:
for (v=0; v < values.length; v += 1) {
valueString = values[v];
value = 0;
if (valueString.startsWith('n')) {
value = BigInt(valueString.substring(1));
} else {
value = Number(valueString);
}
s += 'valueString: ' + valueString + '<br/>';
s += ' type: ' + (typeof value) + '<br/>';
s += ' value: ' + value + '<br/>';
for (j=0; j < isLittleEndians.length; j += 1) {
isLittleEndian = isLittleEndians[j];
s += ' ' + (isLittleEndian?'little':'big') + '<br/>';
for (k=0; k < isIntegers.length; k += 1) {
isInteger = isIntegers[k];
s += ' ' + (isInteger?'integer':'float') + '<br/>';
for (l=0; l < isSigneds.length; l += 1) {
isSigned = isSigneds[l];
s += ' ' + (isSigned?'signed':'unsigned') + '<br/>';
for (i=0; i < bitCounts.length; i += 1) {
bitCount = bitCounts[i];
s += ' ' + bitCount + ': ';
// Process value according to parameters:
try {
// Step 1: convert value to bytes:
bytes = ieee754.setBytes(value, bitCount, isLittleEndian, isInteger, isSigned);
// Display conversion result
if (bytes === null) {
s += 'null';
} else {
s += '[' + bytes + ']';
}
// Step 2: where applicable, convert bytes to value:
if (bytes !== null) {
returnValue = ieee754.setValue(bytes, bitCount, isLittleEndian, isInteger, isSigned);
s += ' '
s += '>>>>>> ' + returnValue;
}
} catch(ex) {
s += ' ' + ex.message;
}
s += '<br/>';
}
}
}
}
// Line break between values
s += '<br/>';
}
// Display result:
document.body.innerHTML = s;
</script>
</body>
</html>