Skip to content

Commit

Permalink
fix some layeradd10 edge case for small numbers and -1.pow10() (fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Patashu committed Nov 23, 2021
1 parent b20ef44 commit f6beb83
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 14 deletions.
17 changes: 14 additions & 3 deletions break_eternity.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ var Decimal = /*#__PURE__*/function () {
if (a.layer === 0) {
var newmag = Math.pow(10, a.sign * a.mag);

if (Number.isFinite(newmag) && Math.abs(newmag) > 0.1) {
if (Number.isFinite(newmag) && Math.abs(newmag) >= 0.1) {
return FC(1, 0, newmag);
} else {
if (a.sign === 0) {
Expand All @@ -1538,11 +1538,11 @@ var Decimal = /*#__PURE__*/function () {
} //handle all 4 layer 1+ cases individually


if (a.sign > 0 && a.mag > 0) {
if (a.sign > 0 && a.mag >= 0) {
return FC(a.sign, a.layer + 1, a.mag);
}

if (a.sign < 0 && a.mag > 0) {
if (a.sign < 0 && a.mag >= 0) {
return FC(-a.sign, a.layer + 1, -a.mag);
} //both the negative mag cases are identical: one +/- rounding error

Expand Down Expand Up @@ -1940,6 +1940,17 @@ var Decimal = /*#__PURE__*/function () {
var result = D(this);

if (diff >= 1) {
//bug fix: if result is very smol (mag < 0, layer > 0) turn it into 0 first
if (result.mag < 0 && result.layer > 0) {
result.sign = 0;
result.mag = 0;
result.layer = 0;
} else if (result.sign === -1 && result.layer == 0) {
//bug fix - for stuff like -3.layeradd10(1) we need to move the sign to the mag
result.sign = 1;
result.mag = -result.mag;
}

var layeradd = Math.trunc(diff);
diff -= layeradd;
result.layer += layeradd;
Expand Down
17 changes: 14 additions & 3 deletions break_eternity.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,7 @@ var Decimal = /*#__PURE__*/function () {
if (a.layer === 0) {
var newmag = Math.pow(10, a.sign * a.mag);

if (Number.isFinite(newmag) && Math.abs(newmag) > 0.1) {
if (Number.isFinite(newmag) && Math.abs(newmag) >= 0.1) {
return FC(1, 0, newmag);
} else {
if (a.sign === 0) {
Expand All @@ -1536,11 +1536,11 @@ var Decimal = /*#__PURE__*/function () {
} //handle all 4 layer 1+ cases individually


if (a.sign > 0 && a.mag > 0) {
if (a.sign > 0 && a.mag >= 0) {
return FC(a.sign, a.layer + 1, a.mag);
}

if (a.sign < 0 && a.mag > 0) {
if (a.sign < 0 && a.mag >= 0) {
return FC(-a.sign, a.layer + 1, -a.mag);
} //both the negative mag cases are identical: one +/- rounding error

Expand Down Expand Up @@ -1938,6 +1938,17 @@ var Decimal = /*#__PURE__*/function () {
var result = D(this);

if (diff >= 1) {
//bug fix: if result is very smol (mag < 0, layer > 0) turn it into 0 first
if (result.mag < 0 && result.layer > 0) {
result.sign = 0;
result.mag = 0;
result.layer = 0;
} else if (result.sign === -1 && result.layer == 0) {
//bug fix - for stuff like -3.layeradd10(1) we need to move the sign to the mag
result.sign = 1;
result.mag = -result.mag;
}

var layeradd = Math.trunc(diff);
diff -= layeradd;
result.layer += layeradd;
Expand Down
17 changes: 14 additions & 3 deletions break_eternity.js
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,7 @@
if (a.layer === 0) {
var newmag = Math.pow(10, a.sign * a.mag);

if (Number.isFinite(newmag) && Math.abs(newmag) > 0.1) {
if (Number.isFinite(newmag) && Math.abs(newmag) >= 0.1) {
return FC(1, 0, newmag);
} else {
if (a.sign === 0) {
Expand All @@ -1542,11 +1542,11 @@
} //handle all 4 layer 1+ cases individually


if (a.sign > 0 && a.mag > 0) {
if (a.sign > 0 && a.mag >= 0) {
return FC(a.sign, a.layer + 1, a.mag);
}

if (a.sign < 0 && a.mag > 0) {
if (a.sign < 0 && a.mag >= 0) {
return FC(-a.sign, a.layer + 1, -a.mag);
} //both the negative mag cases are identical: one +/- rounding error

Expand Down Expand Up @@ -1944,6 +1944,17 @@
var result = D(this);

if (diff >= 1) {
//bug fix: if result is very smol (mag < 0, layer > 0) turn it into 0 first
if (result.mag < 0 && result.layer > 0) {
result.sign = 0;
result.mag = 0;
result.layer = 0;
} else if (result.sign === -1 && result.layer == 0) {
//bug fix - for stuff like -3.layeradd10(1) we need to move the sign to the mag
result.sign = 1;
result.mag = -result.mag;
}

var layeradd = Math.trunc(diff);
diff -= layeradd;
result.layer += layeradd;
Expand Down
2 changes: 1 addition & 1 deletion break_eternity.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "break_eternity.js",
"version": "1.2.3",
"version": "1.2.4",
"description": "A Javascript numerical library to represent numbers as large as 10^^1e308 and as small as 10^-10^^1e308. Sequel to break_infinity.js, designed for incremental games.",
"main": "dist/break_infinity.js",
"module": "dist/break_infinity.esm.js",
Expand Down
15 changes: 12 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2210,7 +2210,7 @@ export default class Decimal {
//handle layer 0 case - if no precision is lost just use Math.pow, else promote one layer
if (a.layer === 0) {
const newmag = Math.pow(10, a.sign * a.mag);
if (Number.isFinite(newmag) && Math.abs(newmag) > 0.1) {
if (Number.isFinite(newmag) && Math.abs(newmag) >= 0.1) {
return FC(1, 0, newmag);
} else {
if (a.sign === 0) {
Expand All @@ -2222,10 +2222,10 @@ export default class Decimal {
}

//handle all 4 layer 1+ cases individually
if (a.sign > 0 && a.mag > 0) {
if (a.sign > 0 && a.mag >= 0) {
return FC(a.sign, a.layer + 1, a.mag);
}
if (a.sign < 0 && a.mag > 0) {
if (a.sign < 0 && a.mag >= 0) {
return FC(-a.sign, a.layer + 1, -a.mag);
}
//both the negative mag cases are identical: one +/- rounding error
Expand Down Expand Up @@ -2637,6 +2637,15 @@ export default class Decimal {
diff = Decimal.fromValue_noAlloc(diff).toNumber();
const result = D(this);
if (diff >= 1) {
//bug fix: if result is very smol (mag < 0, layer > 0) turn it into 0 first
if (result.mag < 0 && result.layer > 0) {
result.sign = 0; result.mag = 0; result.layer = 0;
}
else if (result.sign === -1 && result.layer == 0) {
//bug fix - for stuff like -3.layeradd10(1) we need to move the sign to the mag
result.sign = 1;
result.mag = -result.mag;
}
const layeradd = Math.trunc(diff);
diff -= layeradd;
result.layer += layeradd;
Expand Down

0 comments on commit f6beb83

Please sign in to comment.