-
Notifications
You must be signed in to change notification settings - Fork 0
/
Long.js
74 lines (74 loc) · 2.38 KB
/
Long.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
class Long{
v=[];
len=0;
sign=1;
constructor(){
if(!arguments[0]){
throw "Error arguments"
}
if(arguments[0].v){
this.v=arguments[0].v;
this.len=arguments[0].len//小数点位数[0,∞)
this.sign=arguments[0].sign;//正负号1,-1
}else if(Object.prototype.toString.call(arguments[0]).slice(8,-1)=="String"){
this.len=arguments[0].split(".")[1]?arguments[0].split(".")[1].length:0;
this.v=arguments[0].replace(".","").replace("-","").split("").map(k=>Number(k)).reverse()//倒序
this.sign=arguments[0].includes("-")?-1:1;
}
return this;
}
add(...num){
for(var j=0;j<num.length;j++){
var f=num[j].v?num[j]:new Long(num[j]);
if(f.len>this.len){
for(var i=0;i<f.len-this.len;i++){this.v.unshift(0)}
}else if(f.len<this.len){
for(var i=0;i<this.len-f.len;i++){f.v.unshift(0)}
}
var enter=0;//进位
for(var i=0;i<f.v.length;i++){
if(!this.v[i]){this.v[i]=0}
this.v[i]+=f.v[i]+enter;
enter=(this.v[i]-this.v[i]%10)/10;
this.v[i]%=10;
}
if(enter!=0){
var lk=0;
while(enter!=0){
if(!this.v[f.v.length+lk]){
this.v[f.v.length+lk]=enter;
enter=0;
break;
}
this.v[f.v.length+lk]+=enter;
enter=(this.v[f.v.length+lk]-this.v[f.v.length+lk]%10)/10;
this.v[f.v.length+lk]%=10;
lk++;
}
}
this.len=f.len>this.len?f.len:this.len;
};
return this;
}
reduce(...num){}
than(num){
var f=num[j].v?num[j]:new Long(num[j]);
}
int(){
var res=this.sign+1?"":"-";
for(var h=this.v.length-1;h>=this.len;h--){
res+=this.v[h]
}
return new Long(res);
}
float(){}
toString(){
var res=this.sign+1?"":"-";
for(var h=0;h<this.v.length;h++){
res+=this.v[this.v.length-1-h]
if(this.len&&this.v.length-1-h==this.len){res+='.'}
}
return res;
}
toNumber(){return Number(this.toString())}
}