Skip to content

Commit

Permalink
Merge pull request #158 from MathCookie17/master
Browse files Browse the repository at this point in the history
Negative infinity fix
  • Loading branch information
Patashu authored Feb 20, 2024
2 parents 1f7c7ce + 97088f6 commit 5e6beae
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 35 deletions.
24 changes: 16 additions & 8 deletions break_eternity.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,11 @@ var Decimal = /*#__PURE__*/function () {
Any 0 is totally zero (0, 0, 0) and any NaN is totally NaN (NaN, NaN, NaN).
Anything layer 0 has mag 0 OR mag > 1/9e15 and < 9e15.
Anything layer 1 or higher has abs(mag) >= 15.954 and < 9e15.
Any positive infinity is (1, Infinity, Infinity) and any negative infinity is (-1, Infinity, Infinity).
Any positive infinity is (1, Infinity, Infinity) and any negative infinity is (-1, -Infinity, -Infinity).
We will assume in calculations that all Decimals are either erroneous or satisfy these criteria. (Otherwise: Garbage in, garbage out.)
*/
//Any 0 is totally 0
if (this.sign === 0 || this.mag === 0 && this.layer === 0 || this.mag === Number.NEGATIVE_INFINITY && this.layer > 0) {
if (this.sign === 0 || this.mag === 0 && this.layer === 0 || this.mag === Number.NEGATIVE_INFINITY && this.layer > 0 && Number.isFinite(this.layer)) {
this.sign = 0;
this.mag = 0;
this.layer = 0;
Expand All @@ -507,9 +507,14 @@ var Decimal = /*#__PURE__*/function () {
this.sign = -this.sign;
}
//Handle infinities
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY) {
this.mag = Number.POSITIVE_INFINITY;
this.layer = Number.POSITIVE_INFINITY;
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY || this.mag === Number.NEGATIVE_INFINITY || this.layer === Number.NEGATIVE_INFINITY) {
if (this.sign == 1) {
this.mag = Number.POSITIVE_INFINITY;
this.layer = Number.POSITIVE_INFINITY;
} else if (this.sign == -1) {
this.mag = Number.NEGATIVE_INFINITY;
this.layer = Number.NEGATIVE_INFINITY;
}
return this;
}
//Handle shifting from layer 0 to negative layers.
Expand Down Expand Up @@ -930,8 +935,11 @@ var Decimal = /*#__PURE__*/function () {
}, {
key: "toNumber",
value: function toNumber() {
if (this.mag === Number.POSITIVE_INFINITY && this.layer === Number.POSITIVE_INFINITY) {
return this.sign > 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
if (this.mag === Number.POSITIVE_INFINITY && this.layer === Number.POSITIVE_INFINITY && this.sign === 1) {
return Number.POSITIVE_INFINITY;
}
if (this.mag === Number.NEGATIVE_INFINITY && this.layer === Number.NEGATIVE_INFINITY && this.sign === -1) {
return Number.NEGATIVE_INFINITY;
}
if (!Number.isFinite(this.layer)) {
return Number.NaN;
Expand Down Expand Up @@ -980,7 +988,7 @@ var Decimal = /*#__PURE__*/function () {
if (isNaN(this.layer) || isNaN(this.sign) || isNaN(this.mag)) {
return "NaN";
}
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY) {
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY || this.mag === Number.NEGATIVE_INFINITY || this.layer === Number.NEGATIVE_INFINITY) {
return this.sign === 1 ? "Infinity" : "-Infinity";
}
if (this.layer === 0) {
Expand Down
24 changes: 16 additions & 8 deletions break_eternity.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,11 +489,11 @@ var Decimal = /*#__PURE__*/function () {
Any 0 is totally zero (0, 0, 0) and any NaN is totally NaN (NaN, NaN, NaN).
Anything layer 0 has mag 0 OR mag > 1/9e15 and < 9e15.
Anything layer 1 or higher has abs(mag) >= 15.954 and < 9e15.
Any positive infinity is (1, Infinity, Infinity) and any negative infinity is (-1, Infinity, Infinity).
Any positive infinity is (1, Infinity, Infinity) and any negative infinity is (-1, -Infinity, -Infinity).
We will assume in calculations that all Decimals are either erroneous or satisfy these criteria. (Otherwise: Garbage in, garbage out.)
*/
//Any 0 is totally 0
if (this.sign === 0 || this.mag === 0 && this.layer === 0 || this.mag === Number.NEGATIVE_INFINITY && this.layer > 0) {
if (this.sign === 0 || this.mag === 0 && this.layer === 0 || this.mag === Number.NEGATIVE_INFINITY && this.layer > 0 && Number.isFinite(this.layer)) {
this.sign = 0;
this.mag = 0;
this.layer = 0;
Expand All @@ -505,9 +505,14 @@ var Decimal = /*#__PURE__*/function () {
this.sign = -this.sign;
}
//Handle infinities
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY) {
this.mag = Number.POSITIVE_INFINITY;
this.layer = Number.POSITIVE_INFINITY;
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY || this.mag === Number.NEGATIVE_INFINITY || this.layer === Number.NEGATIVE_INFINITY) {
if (this.sign == 1) {
this.mag = Number.POSITIVE_INFINITY;
this.layer = Number.POSITIVE_INFINITY;
} else if (this.sign == -1) {
this.mag = Number.NEGATIVE_INFINITY;
this.layer = Number.NEGATIVE_INFINITY;
}
return this;
}
//Handle shifting from layer 0 to negative layers.
Expand Down Expand Up @@ -928,8 +933,11 @@ var Decimal = /*#__PURE__*/function () {
}, {
key: "toNumber",
value: function toNumber() {
if (this.mag === Number.POSITIVE_INFINITY && this.layer === Number.POSITIVE_INFINITY) {
return this.sign > 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
if (this.mag === Number.POSITIVE_INFINITY && this.layer === Number.POSITIVE_INFINITY && this.sign === 1) {
return Number.POSITIVE_INFINITY;
}
if (this.mag === Number.NEGATIVE_INFINITY && this.layer === Number.NEGATIVE_INFINITY && this.sign === -1) {
return Number.NEGATIVE_INFINITY;
}
if (!Number.isFinite(this.layer)) {
return Number.NaN;
Expand Down Expand Up @@ -978,7 +986,7 @@ var Decimal = /*#__PURE__*/function () {
if (isNaN(this.layer) || isNaN(this.sign) || isNaN(this.mag)) {
return "NaN";
}
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY) {
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY || this.mag === Number.NEGATIVE_INFINITY || this.layer === Number.NEGATIVE_INFINITY) {
return this.sign === 1 ? "Infinity" : "-Infinity";
}
if (this.layer === 0) {
Expand Down
24 changes: 16 additions & 8 deletions break_eternity.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,11 @@
Any 0 is totally zero (0, 0, 0) and any NaN is totally NaN (NaN, NaN, NaN).
Anything layer 0 has mag 0 OR mag > 1/9e15 and < 9e15.
Anything layer 1 or higher has abs(mag) >= 15.954 and < 9e15.
Any positive infinity is (1, Infinity, Infinity) and any negative infinity is (-1, Infinity, Infinity).
Any positive infinity is (1, Infinity, Infinity) and any negative infinity is (-1, -Infinity, -Infinity).
We will assume in calculations that all Decimals are either erroneous or satisfy these criteria. (Otherwise: Garbage in, garbage out.)
*/
//Any 0 is totally 0
if (this.sign === 0 || this.mag === 0 && this.layer === 0 || this.mag === Number.NEGATIVE_INFINITY && this.layer > 0) {
if (this.sign === 0 || this.mag === 0 && this.layer === 0 || this.mag === Number.NEGATIVE_INFINITY && this.layer > 0 && Number.isFinite(this.layer)) {
this.sign = 0;
this.mag = 0;
this.layer = 0;
Expand All @@ -511,9 +511,14 @@
this.sign = -this.sign;
}
//Handle infinities
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY) {
this.mag = Number.POSITIVE_INFINITY;
this.layer = Number.POSITIVE_INFINITY;
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY || this.mag === Number.NEGATIVE_INFINITY || this.layer === Number.NEGATIVE_INFINITY) {
if (this.sign == 1) {
this.mag = Number.POSITIVE_INFINITY;
this.layer = Number.POSITIVE_INFINITY;
} else if (this.sign == -1) {
this.mag = Number.NEGATIVE_INFINITY;
this.layer = Number.NEGATIVE_INFINITY;
}
return this;
}
//Handle shifting from layer 0 to negative layers.
Expand Down Expand Up @@ -934,8 +939,11 @@
}, {
key: "toNumber",
value: function toNumber() {
if (this.mag === Number.POSITIVE_INFINITY && this.layer === Number.POSITIVE_INFINITY) {
return this.sign > 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
if (this.mag === Number.POSITIVE_INFINITY && this.layer === Number.POSITIVE_INFINITY && this.sign === 1) {
return Number.POSITIVE_INFINITY;
}
if (this.mag === Number.NEGATIVE_INFINITY && this.layer === Number.NEGATIVE_INFINITY && this.sign === -1) {
return Number.NEGATIVE_INFINITY;
}
if (!Number.isFinite(this.layer)) {
return Number.NaN;
Expand Down Expand Up @@ -984,7 +992,7 @@
if (isNaN(this.layer) || isNaN(this.sign) || isNaN(this.mag)) {
return "NaN";
}
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY) {
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY || this.mag === Number.NEGATIVE_INFINITY || this.layer === Number.NEGATIVE_INFINITY) {
return this.sign === 1 ? "Infinity" : "-Infinity";
}
if (this.layer === 0) {
Expand Down
2 changes: 1 addition & 1 deletion break_eternity.min.js

Large diffs are not rendered by default.

27 changes: 17 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1552,12 +1552,12 @@ export default class Decimal {
Any 0 is totally zero (0, 0, 0) and any NaN is totally NaN (NaN, NaN, NaN).
Anything layer 0 has mag 0 OR mag > 1/9e15 and < 9e15.
Anything layer 1 or higher has abs(mag) >= 15.954 and < 9e15.
Any positive infinity is (1, Infinity, Infinity) and any negative infinity is (-1, Infinity, Infinity).
Any positive infinity is (1, Infinity, Infinity) and any negative infinity is (-1, -Infinity, -Infinity).
We will assume in calculations that all Decimals are either erroneous or satisfy these criteria. (Otherwise: Garbage in, garbage out.)
*/

//Any 0 is totally 0
if (this.sign === 0 || (this.mag === 0 && this.layer === 0) || (this.mag === Number.NEGATIVE_INFINITY && this.layer > 0)) {
if (this.sign === 0 || (this.mag === 0 && this.layer === 0) || (this.mag === Number.NEGATIVE_INFINITY && this.layer > 0 && Number.isFinite(this.layer))) {
this.sign = 0;
this.mag = 0;
this.layer = 0;
Expand All @@ -1571,9 +1571,15 @@ export default class Decimal {
}

//Handle infinities
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY) {
this.mag = Number.POSITIVE_INFINITY;
this.layer = Number.POSITIVE_INFINITY;
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY || this.mag === Number.NEGATIVE_INFINITY || this.layer === Number.NEGATIVE_INFINITY) {
if (this.sign == 1) {
this.mag = Number.POSITIVE_INFINITY;
this.layer = Number.POSITIVE_INFINITY;
}
else if (this.sign == -1) {
this.mag = Number.NEGATIVE_INFINITY
this.layer = Number.NEGATIVE_INFINITY;
}
return this;
}

Expand Down Expand Up @@ -2009,10 +2015,11 @@ export default class Decimal {
* Returns the numeric value of the Decimal it's called on. Will return Infinity (or -Infinity for negatives) for Decimals that are larger than Number.MAX_VALUE.
*/
public toNumber(): number {
if (this.mag === Number.POSITIVE_INFINITY && this.layer === Number.POSITIVE_INFINITY) {
return this.sign > 0
? Number.POSITIVE_INFINITY
: Number.NEGATIVE_INFINITY;
if (this.mag === Number.POSITIVE_INFINITY && this.layer === Number.POSITIVE_INFINITY && this.sign === 1) {
return Number.POSITIVE_INFINITY;
}
if (this.mag === Number.NEGATIVE_INFINITY && this.layer === Number.NEGATIVE_INFINITY && this.sign === -1) {
return Number.NEGATIVE_INFINITY;
}
if (!Number.isFinite(this.layer)) {
return Number.NaN;
Expand Down Expand Up @@ -2066,7 +2073,7 @@ export default class Decimal {
if (isNaN(this.layer) || isNaN(this.sign) || isNaN(this.mag)) {
return "NaN";
}
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY) {
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY || this.mag === Number.NEGATIVE_INFINITY || this.layer === Number.NEGATIVE_INFINITY) {
return this.sign === 1 ? "Infinity" : "-Infinity";
}

Expand Down

0 comments on commit 5e6beae

Please sign in to comment.