/
index.js
212 lines (210 loc) · 7.22 KB
/
index.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
'use strict'
const regex = /[1-9]0+$/
class bigN {
constructor (number, numDecimal) {
number = number || 0
let decimal = [ ]
this.number = number.toString().split('.')
this.number[ 1 ] && (decimal = this.number[ 1 ].split(''))
this.decimal = numDecimal || decimal.length
this.number = this.number[ 0 ].split('')
this.number = this.number.concat(decimal)
}
/**
* sum method
*
*/
add (number) {
console.log('number', number)
let num = number.toString().split('.')
console.log('num', num)
let decimal = [ ]
num[ 1 ] && (decimal = num[ 1 ].split(''))
num = num[ 0 ].split('')
num = num.concat(decimal)
let a, b
let _res = 0,
res = [ ]
console.log('this.decimal', this.decimal)
let numDecimal = decimal.length > this.decimal ? decimal.length : this.decimal
console.log('numDecimal', numDecimal)
let max = numDecimal + ((num.length - decimal.length) > (this.number.length - this.decimal)
? num.length - decimal.length : this.number.length - this.decimal)
console.log('max', max)
let numMax = this.number.length - this.decimal + numDecimal
console.log('numMax', numMax)
let numMax2 = num.length - decimal.length + numDecimal
console.log('numMax2', numMax2)
console.log('this.number', this.number)
for (let i = 1; i <= max; i++) {
a = this.number[ numMax - i ] !== undefined ? +(this.number[ numMax - i ]) : 0
console.log('a', a)
b = num[ numMax2 - i ] !== undefined ? +(num[ numMax2 - i ]) : 0
console.log('b', b)
res[ max - i ] = a + b + _res
console.log('res', res)
if (res[ max - i ] > 9) {
_res = 1
res[ max - i ] = res[ max - i ] % 10
} else {
_res = 0
}
}
this.decimal = numDecimal
this.number = res
let zeros = this.number.join('').search(regex)
zeros > -1
&& (this.number = this.number.slice(0, zeros + 1))
&& (this.decimal = this.decimal + zeros - this.number.length)
zeros = this.number.reverse().join('').search(regex)
this.number.reverse()
zeros > -1 && (this.number = this.number.slice(this.number.length - zeros - 1))
return this
}
/**
* multiply method
*
*/
multiply (number) {
let decimal = [ ]
let num = number.toString().split('.')
num[ 1 ] && (decimal = num[ 1 ].split(''))
num = num[ 0 ].split('')
num = num.concat(decimal)
let numDecimal = decimal.length + this.decimal
let max = num.length + this.number.length
let res = [ ],
_res,
a, b, c
for (var i = 1; i <= this.number.length; i++) {
_res = 0
a = +(this.number[ this.number.length - i ])
for (var j = 1; j <= num.length; j++) {
b = +(num[ num.length - j ] ? num[ num.length - j ] : 0)
c = (i === 1) ? 0 : +(res[ max - j - i + 1 ]) || 0
res[ max - j - i + 1 ] = a * b + _res + c
if (res[ max - j - i + 1 ] > 9 && j !== num.length) {
_res = Math.floor(res[ max - j - i + 1 ] / 10)
res[ max - j - i + 1 ] = res[ max - j - i + 1 ] % 10
} else if (res[ max - j - i + 1 ] > 9 && j === num.length) {
res.splice.apply(res, [ max - j - i + 2 - res[ max - j - i + 1 ].toString()
.length, res[ max - j - i + 1 ].toString()
.length
].concat(res[ max - j - i + 1 ].toString().split('')))
} else {
_res = 0
}
}
}
this.decimal = numDecimal
this.number = res;
let zeros = this.number.join('').search(regex)
zeros > -1
&& (this.number = this.number.slice(0, zeros + 1))
&& (this.decimal = this.decimal + zeros - this.number.length)
zeros = this.number.reverse().join('').search(regex)
this.number.reverse()
zeros > -1 && (this.number = this.number.slice(this.number.length - zeros - 1))
return this
}
/**
* rest method
*
*/
rest (number) {
let num = number.toString().split('.')
let decimal = [ ]
num[ 1 ] && (decimal = num[ 1 ].split(''))
num = num[ 0 ].split('')
num = num.concat(decimal)
let a, b
let _res = 0,
res = [ ]
let numDecimal = decimal.length > this.decimal ? decimal.length : this.decimal
let max = numDecimal + ((num.length - decimal.length) > (this.number.length - this.decimal)
? num.length - decimal.length : this.number.length - this.decimal)
let numMax = this.number.length - this.decimal + numDecimal
let numMax2 = num.length - decimal.length + numDecimal
for (var i = 1; i <= max; i++) {
a = this.number[ numMax - i ] !== undefined ? +(this.number[ numMax - i ]) : 0
b = num[ numMax2 - i ] !== undefined ? +(num[ numMax2 - i ]) : 0
console.log('a,b', a, b, _res)
if (a >= b) {
res[ max - i ] = a - b - _res
_res = 0
} else {
res[ max - i ] = a + 10 - b - _res
_res = 1
}
console.log('res',res)
}
this.decimal = numDecimal
this.number = res
let zeros = this.number.join('').search(regex)
zeros > -1
&& (this.number = this.number.slice(0, zeros + 1))
&& (this.decimal = this.decimal + zeros - this.number.length)
zeros = this.number.reverse().join('').search(regex)
this.number.reverse()
zeros > -1 && (this.number = this.number.slice(this.number.length - zeros - 1))
return this
}
/**
* divition method
*
*/
div (number, numDecimal) {
console.log('this.number', number, this.number, this.decimal)
let num = number.toString().split('.')
let decimal = [ ]
num[ 1 ] && (decimal = num[ 1 ].split(''))
num = num[ 0 ].split('')
num = num.concat(decimal)
let min, max
max = this.number.length
min = num.length
let dif = max - min
dif = dif < 0 ? 0 : dif
let res = [ ],
a, b, _res
numDecimal = numDecimal === undefined ? this.decimal : numDecimal
let n = numDecimal - this.decimal + decimal.length + dif
a = +(this.number.slice(0, dif == 0 ? max : min).join(''))
b = +(num.join(''))
for (var i = 0; i <= n; i++) {
res[ i ] = Math.floor(a / b)
_res = a - b * res[ i ]
a = +(_res.toString().concat(this.number[ i + min ] === undefined
? 0 : this.number[ i + min ]))
if (!_res && this.number[ i + min ] === undefined) { break }
}
console.log('res', res)
this.number = res.slice(0,res.length-this.decimal + decimal.length + numDecimal)
this.decimal = numDecimal
console.log('this.number', this.number)
let zeros = this.number.join('').search(regex)
zeros > -1
&& (this.number = this.number.slice(0, zeros + 1))
&& (this.decimal = this.decimal + zeros - this.number.length)
console.log('this.decimal', this.decimal, this.number)
if((this.number.length === this.decimal + 1) && this.number[0] === 0 ) return this
zeros = this.number.reverse().join('').search(regex)
console.log('zeros', zeros)
this.number.reverse()
zeros > -1 &&
(this.number = this.number.slice(this.number.length - zeros - 1))
console.log('this.number', this.number, this.decimal)
return this
}
/**
* to String method
*
*/
toString () {
return this.number.slice(0, this.number.length - this.decimal)
.concat('.')
.concat(this.number.slice(this.number.length - this.decimal))
.join('')
}
}
module.exports = bigN