-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathieee754.test.js
234 lines (226 loc) · 13.8 KB
/
ieee754.test.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
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
const ieee754 = require('./ieee754');
// Testing get decimal method
test('get half precision decimal from [25553] to be 1000.5', () => {
expect(ieee754.getDecimal([25553], {mode: 'half'})).toBe(1000.5);
});
test('get single precision decimal from [17530, 8192] to be 1000.5', () => {
expect(ieee754.getDecimal([17530, 8192], {mode: 'single'})).toBe(1000.5);
});
test('get double precision decimal from [16527, 17408, 0, 0] to be 1000.5', () => {
expect(ieee754.getDecimal([16527, 17408, 0, 0], {mode: 'double'})).toBe(1000.5);
});
test('get half precision decimal from [99, 209] to be 1000.5', () => {
expect(ieee754.getDecimal([99, 209], {mode: 'half'})).toBe(1000.5);
});
test('get single precision decimal from [68, 122, 32, 0] to be 1000.5', () => {
expect(ieee754.getDecimal([68, 122, 32, 0], {mode: 'single'})).toBe(1000.5);
});
test('get double precision decimal from [64, 143, 68, 0, 0, 0, 0, 0] to be 1000.5', () => {
expect(ieee754.getDecimal([64, 143, 68, 0, 0, 0, 0, 0], {mode: 'double'})).toBe(1000.5);
});
test('get half precision decimal from 0110001111010001 to be 1000.5', () => {
expect(ieee754.getDecimal('0110001111010001', {mode: 'half'})).toBe(1000.5);
});
test('get single precision decimal from 01000100011110100010000000000000 to be 1000.5', () => {
expect(ieee754.getDecimal('01000100011110100010000000000000', {mode: 'single'})).toBe(1000.5);
});
test('get double precision decimal from 0100000010001111010001000000000000000000000000000000000000000000 to be 1000.5', () => {
expect(ieee754.getDecimal('0100000010001111010001000000000000000000000000000000000000000000', {mode: 'double'})).toBe(1000.5);
});
// Testing get precision method
test('get half precision from 1000.5 to be [25553]', () => {
expect(ieee754.getPrecision(1000.5, {mode: 'half', returnType: '16bitArray'})).toStrictEqual([25553]);
});
test('get single precision from 1000.5 to be [17530, 8192]', () => {
expect(ieee754.getPrecision(1000.5, {mode: 'single', returnType: '16bitArray'})).toStrictEqual([17530, 8192]);
});
test('get double precision from 1000.5 to be [16527, 17408, 0, 0]', () => {
expect(ieee754.getPrecision(1000.5, {mode: 'double', returnType: '16bitArray'})).toStrictEqual([16527, 17408, 0, 0]);
});
test('get half precision from 1000.5 to be [99, 209]', () => {
expect(ieee754.getPrecision(1000.5, {mode: 'half', returnType: '8bitArray'})).toStrictEqual([99, 209]);
});
test('get single precision from 1000.5 to be [68, 122, 32, 0]', () => {
expect(ieee754.getPrecision(1000.5, {mode: 'single', returnType: '8bitArray'})).toStrictEqual([68, 122, 32, 0]);
});
test('get double precision from 1000.5 to be [64, 143, 68, 0, 0, 0, 0, 0]', () => {
expect(ieee754.getPrecision(1000.5, {mode: 'double', returnType: '8bitArray'})).toStrictEqual([64, 143, 68, 0, 0, 0, 0, 0]);
});
test('get half precision from 1000.5 to be 0110001111010001', () => {
expect(ieee754.getPrecision(1000.5, {mode: 'half'})).toBe('0110001111010001');
});
test('get single precision from 1000.5 to be 01000100011110100010000000000000', () => {
expect(ieee754.getPrecision(1000.5, {mode: 'single'})).toBe('01000100011110100010000000000000');
});
test('get double precision from 1000.5 to be 0100000010001111010001000000000000000000000000000000000000000000', () => {
expect(ieee754.getPrecision(1000.5, {mode: 'double'})).toBe('0100000010001111010001000000000000000000000000000000000000000000');
});
// Testing get precision method with NaN
test('get half precision from NaN to be [32767]', () => {
expect(ieee754.getPrecision(NaN, {mode: 'half', returnType: '16bitArray'})).toStrictEqual([32767]);
});
test('get single precision from NaN to be [32767, 65535]', () => {
expect(ieee754.getPrecision(NaN, {mode: 'single', returnType: '16bitArray'})).toStrictEqual([32767, 65535]);
});
test('get double precision from NaN to be [32767, 65535, 65535, 65535]', () => {
expect(ieee754.getPrecision(NaN, {mode: 'double', returnType: '16bitArray'})).toStrictEqual([32767, 65535, 65535, 65535]);
});
test('get half precision from NaN to be [127, 255]', () => {
expect(ieee754.getPrecision(NaN, {mode: 'half', returnType: '8bitArray'})).toStrictEqual([127, 255]);
});
test('get single precision from NaN to be [127, 255, 255, 255]', () => {
expect(ieee754.getPrecision(NaN, {mode: 'single', returnType: '8bitArray'})).toStrictEqual([127, 255, 255, 255]);
});
test('get double precision from NaN to be [127, 255, 255, 255, 255, 255, 255, 255]', () => {
expect(ieee754.getPrecision(NaN, {mode: 'double', returnType: '8bitArray'})).toStrictEqual([127, 255, 255, 255, 255, 255, 255, 255]);
});
test('get half precision from NaN to be 0111111111111111', () => {
expect(ieee754.getPrecision(NaN, {mode: 'half'})).toBe('0111111111111111');
});
test('get single precision from NaN to be 01111111111111111111111111111111', () => {
expect(ieee754.getPrecision(NaN, {mode: 'single'})).toBe('01111111111111111111111111111111');
});
test('get double precision from NaN to be 0111111111111111111111111111111111111111111111111111111111111111', () => {
expect(ieee754.getPrecision(NaN, {mode: 'double'})).toBe('0111111111111111111111111111111111111111111111111111111111111111');
});
// Testing get precision method with +Infinity
test('get half precision from +Infinity to be [31744]', () => {
expect(ieee754.getPrecision(Infinity, {mode: 'half', returnType: '16bitArray'})).toStrictEqual([31744]);
});
test('get single precision from +Infinity to be [32640, 0]', () => {
expect(ieee754.getPrecision(Infinity, {mode: 'single', returnType: '16bitArray'})).toStrictEqual([32640, 0]);
});
test('get double precision from +Infinity to be [32752, 0, 0, 0]', () => {
expect(ieee754.getPrecision(Infinity, {mode: 'double', returnType: '16bitArray'})).toStrictEqual([32752, 0, 0, 0]);
});
test('get half precision from +Infinity to be [124, 0]', () => {
expect(ieee754.getPrecision(Infinity, {mode: 'half', returnType: '8bitArray'})).toStrictEqual([124, 0]);
});
test('get single precision from +Infinity to be [127, 128, 0, 0]', () => {
expect(ieee754.getPrecision(Infinity, {mode: 'single', returnType: '8bitArray'})).toStrictEqual([127, 128, 0, 0]);
});
test('get double precision from +Infinity to be [127, 240, 0, 0, 0, 0, 0, 0]', () => {
expect(ieee754.getPrecision(Infinity, {mode: 'double', returnType: '8bitArray'})).toStrictEqual([127, 240, 0, 0, 0, 0, 0, 0]);
});
test('get half precision from +Infinity to be 0111110000000000', () => {
expect(ieee754.getPrecision(Infinity, {mode: 'half'})).toBe('0111110000000000');
});
test('get single precision from +Infinity to be 01111111100000000000000000000000', () => {
expect(ieee754.getPrecision(Infinity, {mode: 'single'})).toBe('01111111100000000000000000000000');
});
test('get double precision from +Infinity to be 0111111111110000000000000000000000000000000000000000000000000000', () => {
expect(ieee754.getPrecision(Infinity, {mode: 'double'})).toBe('0111111111110000000000000000000000000000000000000000000000000000');
});
// Testing get precision method with -Infinity
test('get half precision from -Infinity to be [64512]', () => {
expect(ieee754.getPrecision(-Infinity, {mode: 'half', returnType: '16bitArray'})).toStrictEqual([64512]);
});
test('get single precision from -Infinity to be [65408, 0]', () => {
expect(ieee754.getPrecision(-Infinity, {mode: 'single', returnType: '16bitArray'})).toStrictEqual([65408, 0]);
});
test('get double precision from -Infinity to be [65520, 0, 0, 0]', () => {
expect(ieee754.getPrecision(-Infinity, {mode: 'double', returnType: '16bitArray'})).toStrictEqual([65520, 0, 0, 0]);
});
test('get half precision from -Infinity to be [252, 0]', () => {
expect(ieee754.getPrecision(-Infinity, {mode: 'half', returnType: '8bitArray'})).toStrictEqual([252, 0]);
});
test('get single precision from -Infinity to be [255, 128, 0, 0]', () => {
expect(ieee754.getPrecision(-Infinity, {mode: 'single', returnType: '8bitArray'})).toStrictEqual([255, 128, 0, 0]);
});
test('get double precision from -Infinity to be [255, 240, 0, 0, 0, 0, 0, 0]', () => {
expect(ieee754.getPrecision(-Infinity, {mode: 'double', returnType: '8bitArray'})).toStrictEqual([255, 240, 0, 0, 0, 0, 0, 0]);
});
test('get half precision from -Infinity to be 1111110000000000', () => {
expect(ieee754.getPrecision(-Infinity, {mode: 'half'})).toBe('1111110000000000');
});
test('get single precision from -Infinity to be 11111111100000000000000000000000', () => {
expect(ieee754.getPrecision(-Infinity, {mode: 'single'})).toBe('11111111100000000000000000000000');
});
test('get double precision from -Infinity to be 1111111111110000000000000000000000000000000000000000000000000000', () => {
expect(ieee754.getPrecision(-Infinity, {mode: 'double'})).toBe('1111111111110000000000000000000000000000000000000000000000000000');
});
// Testing get precision method with +0
test('get half precision from +0 to be [0]', () => {
expect(ieee754.getPrecision(0, {mode: 'half', returnType: '16bitArray'})).toStrictEqual([0]);
});
test('get single precision from +0 to be [0, 0]', () => {
expect(ieee754.getPrecision(0, {mode: 'single', returnType: '16bitArray'})).toStrictEqual([0, 0]);
});
test('get double precision from +0 to be [0, 0, 0, 0]', () => {
expect(ieee754.getPrecision(0, {mode: 'double', returnType: '16bitArray'})).toStrictEqual([0, 0, 0, 0]);
});
test('get half precision from +0 to be [0, 0]', () => {
expect(ieee754.getPrecision(0, {mode: 'half', returnType: '8bitArray'})).toStrictEqual([0, 0]);
});
test('get single precision from +0 to be [0, 0, 0, 0]', () => {
expect(ieee754.getPrecision(0, {mode: 'single', returnType: '8bitArray'})).toStrictEqual([0, 0, 0, 0]);
});
test('get double precision from +0 to be [0, 0, 0, 0, 0, 0, 0, 0]', () => {
expect(ieee754.getPrecision(0, {mode: 'double', returnType: '8bitArray'})).toStrictEqual([0, 0, 0, 0, 0, 0, 0, 0]);
});
test('get half precision from +0 to be 0000000000000000', () => {
expect(ieee754.getPrecision(0, {mode: 'half'})).toBe('0000000000000000');
});
test('get single precision from +0 to be 00000000000000000000000000000000', () => {
expect(ieee754.getPrecision(0, {mode: 'single'})).toBe('00000000000000000000000000000000');
});
test('get double precision from +0 to be 0000000000000000000000000000000000000000000000000000000000000000', () => {
expect(ieee754.getPrecision(0, {mode: 'double'})).toBe('0000000000000000000000000000000000000000000000000000000000000000');
});
// Testing get precision method with -0
test('get half precision from -0 to be [32768]', () => {
expect(ieee754.getPrecision(-0, {mode: 'half', returnType: '16bitArray'})).toStrictEqual([32768]);
});
test('get single precision from -0 to be [32768, 0]', () => {
expect(ieee754.getPrecision(-0, {mode: 'single', returnType: '16bitArray'})).toStrictEqual([32768, 0]);
});
test('get double precision from -0 to be [32768, 0, 0, 0]', () => {
expect(ieee754.getPrecision(-0, {mode: 'double', returnType: '16bitArray'})).toStrictEqual([32768, 0, 0, 0]);
});
test('get half precision from -0 to be [128, 0]', () => {
expect(ieee754.getPrecision(-0, {mode: 'half', returnType: '8bitArray'})).toStrictEqual([128, 0]);
});
test('get single precision from -0 to be [128, 0, 0, 0]', () => {
expect(ieee754.getPrecision(-0, {mode: 'single', returnType: '8bitArray'})).toStrictEqual([128, 0, 0, 0]);
});
test('get double precision from -0 to be [128, 0, 0, 0, 0, 0, 0, 0]', () => {
expect(ieee754.getPrecision(-0, {mode: 'double', returnType: '8bitArray'})).toStrictEqual([128, 0, 0, 0, 0, 0, 0, 0]);
});
test('get half precision from -0 to be 1000000000000000', () => {
expect(ieee754.getPrecision(-0, {mode: 'half'})).toBe('1000000000000000');
});
test('get single precision from -0 to be 10000000000000000000000000000000', () => {
expect(ieee754.getPrecision(-0, {mode: 'single'})).toBe('10000000000000000000000000000000');
});
test('get double precision from -0 to be 1000000000000000000000000000000000000000000000000000000000000000', () => {
expect(ieee754.getPrecision(-0, {mode: 'double'})).toBe('1000000000000000000000000000000000000000000000000000000000000000');
});
// Testing to get precision for values under 1
// these might be wrong, 0.3 conversion might be wrong
test('get half precision from 0.1 to be 0010111001100111', () => {
expect(ieee754.getPrecision(0.1, {mode: 'half'})).toBe('0010111001100111');
});
test('get half precision from 0.2 to be 0011001001100111', () => {
expect(ieee754.getPrecision(0.2, {mode: 'half'})).toBe('0011001001100111');
});
test('get half precision decimal from 0011001001100111 to be 0.2000732421875', () => {
expect(ieee754.getDecimal('0011001001100111', {mode: 'half'})).toBe(0.2000732421875);
});
test('get single precision from 0.1 to be 00111101110011001100110011001101', () => {
expect(ieee754.getPrecision(0.1, {mode: 'single'})).toBe('00111101110011001100110011001101');
});
test('get single precision from 0.3 to be 00111110100110011001100110011001', () => {
expect(ieee754.getPrecision(0.3, {mode: 'single'})).toBe('00111110100110011001100110011001');
});
test('get double precision from 0.1 to be 0011111110111001100110011001100110011001100110011001100110011010', () => {
expect(ieee754.getPrecision(0.1, {mode: 'double'})).toBe('0011111110111001100110011001100110011001100110011001100110011010');
});
test('get double precision from 0.5 to be 0011111111100000000000000000000000000000000000000000000000000000', () => {
expect(ieee754.getPrecision(0.5, {mode: 'double'})).toBe('0011111111100000000000000000000000000000000000000000000000000000');
});
test('get double precision from 0.9 to be 0011111111101100110011001100110011001100110011001100110011001101', () => {
expect(ieee754.getPrecision(0.9, {mode: 'double'})).toBe('0011111111101100110011001100110011001100110011001100110011001101');
});
test('get double precision from 1.1 to be 0011111111110001100110011001100110011001100110011001100110011010', () => {
expect(ieee754.getPrecision(1.1, {mode: 'double'})).toBe('0011111111110001100110011001100110011001100110011001100110011010');
});