-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber.ts
More file actions
147 lines (126 loc) · 3.22 KB
/
Copy pathnumber.ts
File metadata and controls
147 lines (126 loc) · 3.22 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
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
function toBinary(number, radix, fix): string {
var f = '0'
if (number < 0) {
number = 0 - number;
f = '1'
}
var r = 0, num = number;
if (num > 1) {
while (num > 1) {
num /= radix;
r++
}
} else {
while (num < 1) {
num *= radix
r--
}
}
// decimals
// fix
num = fix ? parseFloat((num % 1).toPrecision(10)) : num % 1
var total = 52;
var s = '';
while (num !== 0 && total--) {
s += num * radix >= 1 ? 1 : 0
num = (num * radix) % 1
}
// carry bit
if (total < 0 && num * radix >= 1) {
s = (parseInt(s, 2) + 1).toString(2)
s = s.padStart(52, '0')
} else {
s = s.padEnd(52, '0')
}
var e = '';
r += 1023
// while (r >= 1) {
// e = r % radix + e;
// r -= r % radix;
// r /= radix;
// }
e = r.toString(2).padStart(11, '0')
return f + e + s
}
function testBinary(number, fix) {
var b = toBinary(number, 2, fix)
var s = number.toString(2)
console.log(number, b, s)
}
// testBinary(1)
// testBinary(10)
// testBinary(5)
// testBinary(5.2)
// testBinary(0.8)
// testBinary(0.5)
function addBinary(num, add) {
if (!add.includes('1')) {
return num;
}
let n = xor(num, add);
let m = and(num, add) + '0';
return addBinary(n, m)
}
// JavaScript的位运算,会将操作数转化成32位二进制串,精度不够导致结果错误
// 所以这里自己实现异或和与运算
function xor(str1, str2) {
let s = Math.max(str1.length, str2.length)
str1 = str1.padStart(s, '0')
str2 = str2.padStart(s, '0')
let str = '';
while (s--) {
str = (str1[s] !== str2[s] ? '1' : '0') + str
}
return str
}
function and(str1, str2) {
let s = Math.max(str1.length, str2.length)
str1 = str1.padStart(s, '0')
str2 = str2.padStart(s, '0')
let str = '';
while (s--) {
str = ((str1[s] === '1' && str2[s] === '1') ? '1' : '0') + str
}
return str
}
function add(str1, str2) {
let s = Math.max(str1.length, str2.length)
str1 = str1.padStart(s, '0')
str2 = str2.padStart(s, '0')
let str = '', f = 0;
while (s--) {
var cur = f + Number(str1[s]) && Number(str2[s])
str = cur % 2 + str
f = cur > 1 ? 1 : 0
}
return str
}
function add0203() {
testBinary(0.2, true)
testBinary(0.1, true)
testBinary(0.30000000000000004, false)
testBinary(0.3, true)
// 0.1+0.2
console.log(addBinary('1001100110011001100110011001100110011001100110011010', '1100110011001100110011001100110011001100110011001101'))
// 010110011001100110011001100110011001100110011001100111
// 这个地方,首位多了一个1,与原来的默认首位1相加产生进位,于是变成0
// 而末尾多余的1也进位了,0111变成100
console.log(toDecimal('0011001100110011001100110011001100110011001100110100'))
//
console.log(toDecimal('0011001100110011001100110011001100110011001100110100') * Math.pow(2, -2))
}
console.log('0.1+0.2', add0203())
// console.log(addBinary('10', '1101'))
// console.log(addBinary('1001100110011010', '1100110011001101'))
function toDecimal(str) {
let num = 0;
for (let s = 0; s < str.length; s++) {
num += str[s] * Math.pow(2, -1 - s)
}
return 1 + num;
}
function binaryToFloat(str) {
let s = str[0];
let e = str.subString(1, 12)
let base = str.subString(12, 64)
}