Skip to content

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.

License

Notifications You must be signed in to change notification settings

Patashu/break_eternity.js

Repository files navigation

break_eternity.js

A Javascript numerical library to represent numbers as large as 10^^1e308 and as 'small' as 10^-(10^^1e308). This is a sequel to break_infinity.js, my other library which maxes out at 1e1e308 ( https://github.com/Patashu/break_infinity.js ) and its C# port ( https://github.com/Razenpok/BreakInfinity.cs ). Despite handling a wider range of numbers, execution time is comparable (within 2x/0.5x as fast as break_infinity.js in testing) and it has the same interface, so it can be used as a drop-in replacement for break_infinity.js and decimal.js.

Now with arbitrary real height and base handling in your favourite hyper 4 operators (tetrate, iterated exponentiation, iterated logarithm, super logarithm, super root) and even in hyper 5 operators (though hyper 5 operators quickly exceed the library's number limits)! Using an analytic approximation for bases <= 10, but linear approximation for bases > 10 (but there's options to use the linear approximation everywhere if you need consistent behavior) for hyper 4 operators.

The internal representation is as follows: Decimal.fromComponents(sign, layer, mag) === sign*10^10^10^ ... (layer times) mag. So a layer 0 number is just sign*mag, a layer 1 number is sign*10^mag, a layer 2 number is sign*10^10^mag, and so on. If layer > 0 and mag < 0, then the number's exponent is negative, e.g. sign*10^-10^10^10^ ... mag.

  • sign is -1, 0 or 1.
  • layer is a non-negative integer.
  • mag is a Number, normalized as follows: if it is above 9e15, log10(mag) it and increment layer. If it is below log10(9e15) (about 15.954) and layer > 0, Math.pow(10, mag) it and decrement layer. At layer 0, sign is extracted from negative mags. Zeroes (this.sign === 0 || (this.mag === 0 && this.layer === 0)) become 0, 0, 0 in all fields. Any infinities have both mag and layer as positive Infinity.

Create a Decimal with new Decimal(String or Number or Decimal) or with Decimal.fromComponents(sign, layer, mag). Use operations

IMPORTANT NOTE TO PEOPLE CONVERTING FROM break_infinity.js: log/log2/log10/ln now return Decimal not Number! You'll also need to reconsider your string parsing/displaying functions and consider moving e/exponent calls to absLog10. Support for very small numbers has finally been added, so things like tickspeed multiplier being 1e-400 will be fine now!

Functions you can call include abs, neg, round, floor, ceil, trunc, add, sub, mul, div, recip, mod, cmp, cmpabs, max, min, maxabs, minabs, log, log10, ln, pow, root, factorial, gamma, exp, sqrt, tetrate, iteratedexp, iteratedlog, layeradd10, layeradd, slog, ssqrt, lambertw, linear_sroot, pentate, penta_log, linear_penta_root and more! Javascript operators like + and * do not work - you need to call the equivalent functions instead. Note that all these functions return new Decimals - they do not mutate the original Decimal.

Accepted input formats to new Decimal or Decimal.fromString (X, Y, and N represent numbers):

M === M
eX === 10^X
MeX === M*10^X
eXeY === 10^(XeY)
MeXeY === M*10^(XeY)
eeX === 10^10^X
eeXeY === 10^10^(XeY)
eeeX === 10^10^10^X
eeeXeY === 10^10^10^(XeY)
eeee... (N es) X === 10^10^10^ ... (N 10^s) X
(e^N)X === 10^10^10^ ... (N 10^s) X
N PT X === 10^10^10^ ... (N 10^s) X
N PT (X) === 10^10^10^ ... (N 10^s) X
NpX === 10^10^10^ ... (N 10^s) X
FN === 10^10^10^ ... (N 10^s)
XFN === 10^10^10^ ... (N 10^s) X
X^Y === X^Y
X^^N === X^X^X^ ... (N X^s) 1
X^^N;Y === X^X^X^ ... (N X^s) Y
X^^^N === X^^X^^X^^ ... (N X^^s) 1
X^^^N;Y === X^^X^^X^^ ... (N X^^s) Y

Use

The library exports a single function object, Decimal, the constructor of Decimal instances.

It accepts a value of type number, string or Decimal.

x = new Decimal(123.4567);
y = new Decimal("123456.7e-3");
z = new Decimal(x);
x.equals(y) && y.equals(z) && x.equals(z); // true

The methods that return a Decimal can be chained.

x.dividedBy(y).plus(z).times(9).floor();
x.times("1.23456780123456789e+9").plus(9876.5432321).dividedBy("4444562598.111772").ceil();

A list of functions is provided earlier in this readme, or you can read through index.d.ts for a more detailed list.

If you want to just mess around with the library to try it, download break_eternity.js, add <script> </script> tags to the beginning and end respectively, rename it break_eternity.html, open it in chrome, right click -> inspect, go to the Javascript Console, and type in and execute whatever commands you want. Enjoy!


Special thanks:

obligatory SEO: number library, big number, big num, bignumber, bignum, big integer, biginteger, bigint, incremental games, idle games, large numbers, huge numbers


Want to go even further? Check out Naruyoko's OmegaNum.js: https://github.com/Naruyoko/OmegaNum.js

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published