Giant number managemnet

Galdon2004

  • *
  • Posts: 313
So, one of my projects has rather large numbers. I made some overly complicated code to work past the limit of the engine, which overflows somewhere between the quadrillions and quintillions.

I have tried to work around this by making one variable which is "normal" then a second variable which that one rolls up into which counts "quadrillions of the previous variable"  Unfortunately, the method I've used is very clumbsy, buggy, and difficult to work with, changing something which would ordinarily be a few lines of code into a bit of a mess; if statements to check which variables are being used in a calculation, and then doing all the math involved multiple times for each instance. This unfortunately makes every instance of using the main resource very complex, prone to errors, and tedious to update.

I am wondering if anyone has worked with extremely large numbers and knows of a more elegant solution to this issue than what I am currently using.

merrak

  • *
  • Posts: 2726
You can force conversion to floating point, which will allow up to 10^308. Precision will be lost for large enough numbers, but it probably won't be an issue. If you need to go larger, look up 'bigint' on Haxe: https://lib.haxe.org/t/bigint/

Galdon2004

  • *
  • Posts: 313
How do I do that? I had previously been avoiding letting the number become an integer which overflows much lower than the quadrillions, but if there is a way to raise it's limit, I don't know what it is.

merrak

  • *
  • Posts: 2726
Number attributes are floats. I'm not sure where along the line they are being converted to integers.

Some of the blocks convert numbers to integers. Check the 'Preview Code' and see if any blocks put Std.int( ) around the number attribute.


Galdon2004

  • *
  • Posts: 313
Yeah, i've used the numbers as floats, and avoided using operations which would lead to them being turned into integers. I just checked my preview code and it does not have Std.int() anywhere in it in the main behaviors that use the variable. When they were integers they would overflow in the billions, avoiding things that turn them into integers let them go up into the quadrillions, but they overflow far, far from the 10^308 I keep hearing about...

merrak

  • *
  • Posts: 2726
My best guess as to what's happening is that your floating point number is being re-cast as an integer when it is retrieved from a Dynamic variable. A common way this can happen is when you store data in the list or map attributes Stencyl provides, then you retrieve it.

You may need to write your own engine extension to handle storing/retrieving data, since the Stencyl blocks use Dynamic a lot.

Galdon2004

  • *
  • Posts: 313
Yeah, I haven't been using lists or maps either. Is there a list of every type of block that converts to integer? I'm pretty sure I've avoided all of them that I know about, but it seems like somewhere above the quadrillions causes overflows in my code.

I unfortunately don't know how to make an extension.

merrak

  • *
  • Posts: 2726
The blocks I would be wary of are those that use Dynamic type. Unfortunately, that includes several of the most common and useful ones: any blocks that work with data that isn't required to be one specific type. These include the game attribute blocks, the 'get value' blocks in the Behaviors palette, and actor value blocks.

The proper solution would be to not use Dynamic, but that won't be an option if you want to use the blocks. I'm not sure if this would work--but you might be able to store your larger numbers as strings and always concatenate a ".0" when storing them. To do math, you'll need to convert to float (the number attributes are okay for this), do your math, then convert back to a string (with ".0" concatenated) to store the data. Managing the data this way will be significantly slower, but the speed difference is still negligible if you're using huge numbers to store a score, resource counts, or any other data that doesn't need to be accessed thousands of times per frame.

I don't know what Haxe targets this would work on, if any. It's just an idea I pulled off the top of my head. It's a common trick to force floating point math in computer algebra systems (like Mathematica, Wolfram Alpha, Maple) that don't require numeric variables to have types at all... or abstract that from the user.