1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-10-27 20:34:12 +00:00

No longer use bits to express output. Still need to streamline the xBits and yBits (aka remove the usage of toString(2)), but hopefully the owner can help on that part.

This commit is contained in:
Favian Contreras 2014-12-02 14:43:09 -08:00
parent bed0e20df3
commit 316b416bf4

View File

@ -2049,42 +2049,47 @@
return new Decimal(NaN); return new Decimal(NaN);
} }
var x_bits, y_bits, tmp_external = external; var xBits, yBits, tmpExternal = external;
external = false; external = false;
if (x['s'] < 0) { if (x['s'] < 0) {
x_bits = '1' + x['not']()['toString'](2).replace(/[01]/g, function (d) { return +!+d }); xBits = '1' + x['not']()['toString'](2).replace(/[01]/g, function (d) { return +!+d });
} else { } else {
x_bits = '0' + x['trunc']()['toString'](2); xBits = '0' + x['trunc']()['toString'](2);
} }
if (y['s'] < 0) { if (y['s'] < 0) {
y_bits = '1' + y['not']()['toString'](2).replace(/[01]/g, function (d) { return +!+d }); yBits = '1' + y['not']()['toString'](2).replace(/[01]/g, function (d) { return +!+d });
} else { } else {
y_bits = '0' + y['trunc']()['toString'](2); yBits = '0' + y['trunc']()['toString'](2);
} }
external = tmp_external; external = tmpExternal;
if (x_bits.length > y_bits.length) { if (xBits.length > yBits.length) {
y_bits = y_bits.padLeft((y['s'] < 0) ? '1' : '0', x_bits.length); yBits = yBits.padLeft((y['s'] < 0) ? '1' : '0', xBits.length);
} else if (x_bits.length < y_bits.length) { } else if (xBits.length < yBits.length) {
x_bits = x_bits.padLeft((x['s'] < 0) ? '1' : '0', y_bits.length); xBits = xBits.padLeft((x['s'] < 0) ? '1' : '0', yBits.length);
} }
var out_str = ""; var outVal = new Decimal(0);
for (var i = 0; i < x_bits.length; ++i) { var twoPower = Decimal['ONE'];
out_str += "" + func(x_bits[i], y_bits[i]); if (func(xBits[0], yBits[0]) == 0) {
for (var i = xBits.length - 1; i > 0; --i) {
if (func(xBits[i], yBits[i])) {
outVal = outVal['plus'](twoPower);
}
twoPower = twoPower['times'](2);
}
return outVal;
} }
if (out_str[0] == '0') { for (var i = xBits.length - 1; i > 0; --i) {
return new Decimal(out_str, 2); if (func(xBits[i], yBits[i]) == 0) {
outVal = outVal['plus'](twoPower['plus'](Decimal['ONE']));
}
twoPower = twoPower['times'](2);
} }
return outVal['plus'](Decimal['ONE'])['neg']();
var comp_str = "";
for (var i = 1; i < out_str.length; ++i) {
comp_str += (out_str[i] == '0') ? '1' : '0';
}
return new Decimal(comp_str, 2)['plus'](Decimal['ONE'])['neg']();
} }
function coefficientToString(a) { function coefficientToString(a) {
@ -2313,7 +2318,7 @@
r = r || xc[pr + 1] != null; r = r || xc[pr + 1] != null;
if ( rm < 4 if ( rm < 4
? ( i != null || r ) && ( rm == 0 || rm == ( sign < 0 ? 3 : 2 ) ) ? ( i != null || r ) && ( rm == 0 || rm == ( x['s'] < 0 ? 3 : 2 ) )
: i > j || i == j && ( rm == 4 || r || rm == 6 && xc[pr - 1] & 1 || : i > j || i == j && ( rm == 4 || r || rm == 6 && xc[pr - 1] & 1 ||
rm == ( x['s'] < 0 ? 8 : 7 ) ) ) { rm == ( x['s'] < 0 ? 8 : 7 ) ) ) {