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
| // constructeur d'instance(Number|num|null) : this(typeof num)
function num_constructor(v){
if(!v) return this.val=""+(this.neg=this.pos=0),this; // 0
this.neg = (v = v.toExpString()).charAt(0) == '-';
this.val = ((v = v.replace(/[^\d.]/g,'').split('.', 2))[0] = v[0].replace(/^0+/,'') || '0') + (v[1] = v[1] || '',v[1].replace(/0+$/,''));
this.pos = v[1].length;
delete v
}
// constructeur(Number|num|null) : new num
function num(v){
return v instanceof num_constructor?((this instanceof num_constructor)?v.copy():v):new num_constructor(v)
}
// num:prototype
p = num_constructor.prototype = num.prototype;
// "0000000000...00000000000" (^310)
num.MAX_ZERO=(function(max){
for(var i=1,zero='0';i<max;i*=2)
zero+=zero;
return zero.substring(0,max-1)
})(Number(Number.MAX_VALUE.toString().split('e')[1]));
// copy : new num
p.copy = function(){
var v = new num_constructor();
v.neg = this.neg;
v.val = this.val;
v.pos = this.pos;
return v
};
// partie entière : String
p.left = function(){
return this.val.substring(0,this.val.length-this.pos)
};
// partie décimale : String
p.right = function(){
return this.val.substring(this.val.length-this.pos,this.val.length)
};
// valeur complete : String
p.valueOf = p.toString = function(){
return (this.neg?'-':'')+this.left()+"."+this.right()
};
// valeur complete : Number
p.Number = function(){
return Number(this.valueOf())
};
// puissance du nombre : Number
p.power = function(){
return this.val.replace(/^0+/,'').length-this.pos-1
};
// (this<v)=[-1] (this==v)=[0] (this>v)=[1]
p.compare = function(v){
return (this.neg != (v=num(v)).neg)?(this.neg?-1:1):(this.neg?(-this.compareAbs(v)):this.compareAbs(v))
};
// (abs(this)<abs(v))=[-1] etc...
p.compareAbs = function(v){
var a=this,b=num(v);
if(a.power() != b.power())
return ((a.power()>b.power()))?1:-1;
var l=Math.min((a=a.val.split('')).length,(b=b.val.split('')).length),i=-1;
while(++i<l)
if(a[i] != b[i])
return (a[i]>b[i])?1:-1;
return 0;
}
// renvoie une copie "positive" : new num
p.abs = function(){
var r=this.copy();
return r.neg=0,r
};
// supprime les "0" de trop en début et en fin du num : num;
p.clean = function(){
var a=[];
a[0]=this.val.substr(0,this.val.length-this.pos).replace(/^0+/,'') || '0';
a[1]=this.val.substr(this.val.length-this.pos,this.val.length)||'';
a[1]=a[1].replace(/0+$/,'')
return this.pos=a[1].length,this.val=a.join(''),this;
};
// ajoute v au num : new num
p.add = function(v){
var a=this, b=num(v), res=new num_constructor();
res.neg=(a.compareAbs(b)==1?a.neg:b.neg);
res.pos=Math.max(a.pos+b.pos);
res.val=num.MAX_ZERO+Math.abs(
parseInt((a.neg?'-':'')+a.val+num.MAX_ZERO.substr(0,res.pos-a.pos),10)
+parseInt((b.neg?'-':'')+b.val+num.MAX_ZERO.substr(0,res.pos-b.pos),10)
);
return res.clean();
};
// converti un Number en notation (n.e^x) vers la notation classique : String
Number.prototype.toExpString = function(){
var val=this+'',t;
if(!val.match(/^([+-]?\d+(?:.\d+)?)e([-\+]?\d+)$/)) return val;
if(RegExp.$2<0) return ((val<0)?'-':'')+"0."+num.MAX_ZERO.substring(0,-RegExp.$2-1)+Math.abs(RegExp.$1.replace(".", ""));
return RegExp.$1.replace(".", "")+num.MAX_ZERO.substring(0,Number(RegExp.$2)-((t=RegExp.$1.split('.')[1])?t.length:0));
}
// supprime v au num : new num
p.sub = function(b){var r=num(b).copy();return r.neg=1-r.neg,this.add(r);}
// multiplie le num par v : new num
p.multi = function(b){
var a=this, b=num(b), res=new num_constructor();
res.neg=(a.neg^b.neg);
res.pos=a.pos+b.pos;
res.val=(num.MAX_ZERO+(parseInt(a.val,10)*parseInt(b.val,10)).toExpString()).replace('.','');
return res.clean();
}
// divise le num par v : new num
p.div = function(b){
var c,a=this, b=num(b), res=new num_constructor();
res.neg=(a.neg^b.neg);
res.val=num.MAX_ZERO+(parseInt(a.val,10)/parseInt(b.val,10)).toExpString()+num.MAX_ZERO;
res.pos=((c=res.val.split('.')[1])?c.length:num.MAX_ZERO.length)-b.pos+a.pos;
res.val=res.val.replace('.','');
return res.clean();
} |
Partager