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
| function Integer(value){
if(!(this instanceof Integer)){
return new Integer(value);
}
if(value instanceof Integer){
return value;
}
var a = this;
a.originalString = parseFloat(value).toString();
a.pow = a.originalString.indexOf('.');
if(a.pow == -1){
a.pow = a.originalString.length;
}
a.data = a.originalString.replace('.','').split('').map(function(a){return parseInt(a)});
a.negatif = 0;
if(isNaN(a.data[0])){
a.data.shift();
a.pow--;
a.negatif = 1;
}
return a;
}
Integer.prototype.sub = function(value){
return this.add(value,true);
};
Integer.prototype.set = function(data,pow,neg){
var a = this;
a.data = data || [0];
a.pow = pow || 1;
a.negatif = neg || 0;
return a;
};
Integer.prototype.add = function(value,sub){
var a = this, b = Integer(value),
sa = a.negatif, sb = b.negatif,
da = a.data, db = b.data,
la = da.length, lb = db.length,
pa = a.pow, pb = b.pow,
pdiff = pb - pa,
i, compare = a.compare(b);
if(sub){
sb++;
}
if(compare==0 && (sa%2)!=(sb%2) ){
// soustraction lui-même ou même valeur -> return 0
return a.set();
}
if(pdiff>0){
// copy prefix from b
var prefix = db.slice(0, pdiff);
prefix.unshift(0,0);
Array.prototype.splice.apply(da, prefix);
}
if(la < lb){
// copy sufix from b
var sufix = db.slice(la-lb);
sufix.unshift(la+1,0);
Array.prototype.splice.apply(da, sufix);
}
if(compare==-1){
a.negatif=sb;
}
else{
}
for(i=0;i<la && Math.abs(pdiff)+i<da.length;i++){
// add/sub all commun field
if(sa%2 == sb%2){
da[Math.abs(pdiff)+i] += db[Math.max(pdiff,0)+i] || 0;
}
else if(a.negatif%2 == sa%2){
da[Math.abs(pdiff)+i] -= db[Math.max(pdiff,0)+i] || 0;
}
else{
da[Math.abs(pdiff)+i] = (db[Math.max(pdiff,0)+i] || 0) - da[Math.abs(pdiff)+i];
}
}
// set bigger pow
a.pow = Math.max(pa,pb);
// return fixed values
return a.fix();
};
Integer.prototype.fix = function(){
var a = this, da = a.data;
for(i=da.length-1;i>0;i--){
if(da[i]>=10){
da[i] -= 10;
da[i-1]++;
}
else if(da[i]<0){
da[i] += 10;
da[i-1]--;
}
}
if(da[0]>=10){
da[0] -= 10;
da.unshift(1);
a.pow++;
}
if(da[0]<-9){
da[0] += 10;
da.unshift(-1);
a.pow++;
}
if(da[0]<0){
da[0] = Math.abs(da[0]);
a.negatif++;
}
return a;
};
Integer.prototype.compare = function(value){
var a = this, b = Integer(value), i = 0;
if(a==b){
return 0;
}
if(a.pow > b.pow){
return 1;
}
if(a.pow < b.pow){
return -1;
}
var len = Math.max(a.data.length,b.data.length);
for(; a.data[i] == b.data[i] && i<len;i++);
if(a.data[i] > b.data[i] || b.data[i] == undefined){
return 1;
}
if(a.data[i] < b.data[i] || a.data[i] == undefined){
return -1;
}
return 0;
};
Integer.prototype.copy = function(){
var a = this;
return Integer().set(a.data,a.pow,a.negatif);
};
Integer.prototype.mod = function(value){
var a = this, b = Integer(value).copy(), neg = a.negatif;
b.negatif = 0;
a.negatif = 0;
while(a>b){
this.sub(b);
}
a.negatif = neg;
return a;
};
Integer.prototype.toString = function(){
var c = this.data.slice(0);
c.splice(this.pow,0,'.');
return parseFloat((this.negatif%2?'-':'')+c.join(''));
};
writeln = function(){
var a = Array.prototype.slice.call(arguments);
a.push('<br/>');
document.writeln.apply(document,a);
};
var a = -12.311111, b = -12.121111;
writeln( Integer(a).sub(b) );
writeln( a - b );
writeln();
writeln( Integer(5.0).mod(.1) );
writeln( 5.0%.1 ); |
Partager