forked from ren0503/javascript-algorithms-and-data-structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloatAsBinaryString.js
More file actions
61 lines (51 loc) · 2.21 KB
/
Copy pathfloatAsBinaryString.js
File metadata and controls
61 lines (51 loc) · 2.21 KB
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
// @xem: https://en.wikipedia.org/wiki/Single-precision_floating-point_format
const singlePrecisionBytesLength = 4; // 32 bits
// @xem: https://en.wikipedia.org/wiki/Double-precision_floating-point_format
const doublePrecisionBytesLength = 8; // 64 bits
const bitsInByte = 8;
/**
* Chuyển số phẩy động về dạng bit bằng tiêu chuẩn IEEE 754
* @see: https://en.wikipedia.org/wiki/IEEE_754
*
* @param {number} floatNumber - số phẩy động dưới dạng thập phân.
* @param {number} byteLength - số lượng bit dùng để lưu trữ.
* @return {string} - số phẩy động dưới dạng nhị phân.
*/
function floatAsBinaryString(floatNumber, byteLength) {
let numberAsBinaryString = '';
const arrayBuffer = new ArrayBuffer(byteLength);
const dataView = new DataView(arrayBuffer);
const byteOffset = 0;
const littleEndian = false;
if (byteLength === singlePrecisionBytesLength) {
dataView.setFloat32(byteOffset, floatNumber, littleEndian);
} else {
dataView.setFloat64(byteOffset, floatNumber, littleEndian);
}
for (let byteIndex = 0; byteIndex < byteLength; byteIndex += 1) {
let bits = dataView.getUint8(byteIndex).toString(2);
if (bits.length < bitsInByte) {
bits = new Array(bitsInByte - bits.length).fill('0').join('') + bits;
}
numberAsBinaryString += bits;
}
return numberAsBinaryString;
}
/**
* Chuyển số phẩy động thành biểu diễn nhị phân 64 bit bằng tiêu chuẩn IEEE 754
*
* @param {number} floatNumber - số phẩy động dưới dạng thập phân.
* @return {string} - biểu diễn nhị phân 64 bit của số phẩy động.
*/
export function floatAs64BinaryString(floatNumber) {
return floatAsBinaryString(floatNumber, doublePrecisionBytesLength);
}
/**
* Chuyển số phẩy động thành biểu diễn nhị phân 32 bit bằng tiêu chuẩn IEEE 754
*
* @param {number} floatNumber - số phẩy động dưới dạng thập phân.
* @return {string} - biểu diễn nhị phân 32 bit của số phẩy động.
*/
export function floatAs32BinaryString(floatNumber) {
return floatAsBinaryString(floatNumber, singlePrecisionBytesLength);
}