Plugin for incremental games that needs to deal with massive numbers. Ported from break_infinity.js.
In Godot, there are 2 types to represent numbers:
intcan store numbers up to 2^63 - 1 but doesn't work on fractionalsfloatgoes up to 1.8e308, but loses precision on higher numbers
Solution: just combine them
class Decimal:
var mantissa: float # double in C++ (8 bytes)
var exponent: int # int64_t in C++ (8 bytes)
# essentially, the stored number is equal to: mantissa × 10^exponent
# so the upper bound would be 1e(2^63), a number with 9223372036854775808 digitsThis plugin adds the aforementioned Decimal class, along with several dozen functions to do math with them.
It's also designed with speed in mind, as it's written with GDExtension, and only allocates on stack (outside of stringification).
Anyways, this lets us quite literally break infinity™️.
func _ready() -> void:
var d1 := Decimal.from_float(3e200)
var d2 := Decimal.from_float(2e250)
var result := Decimal.mul(d1, d2)
print(
Decimal.to_string(d1), " * ",
Decimal.to_string(d2), " = ",
Decimal.to_string(result)
) # prints "3.e+200 * 2.e+250 = 6.e+450"
# floats would just give you infinity in this case
print(3e200 * 2e250) # prints "inf"This repo is automatically built and mirrored to peachey2k2/break-nihility-bin, which you can just copy into your own game.
i dug a bit and found a couple addons/scripts that do a similar thing
- break-nihility - obama medal meme
- GodotBigNumberClass - same idea but in GDScript, also can do cool formatting
- numberclass-gds - smaller plugin, less bloated
Also numberclass-gds is a GDScript port of NumberClass. I did try to patch it to work with Godot but it performs worse for most things due to GDScript-C# bridge.
