Author Topic: Big number library?  (Read 1216 times)

Is there a big number library that I can use that will support up to 12 or more number digits?

Do you need to work with floats or integers? If it's just integers then using the normal float math operators (+ - * / %) and (implicitly) casting to integer by using a no-op bitwise operation will be fine for most value ranges.

The reason that 999999 + 1 gives you 1e+006 is because you're converting it to a string when you try to display it.

Converting a float to a string in TorqueScript is inaccurate because it uses %g instead of %f to print it, which will automatically switch to scientific notation when the value is large.
Converting an integer to a string in TorqueScript is just fine because it uses %d/%i.

Example:

%a = 999999 + 1;
echo(%a); // 1e+006, %a (float) is converted to string because functions can only take strings as arguments
echo(%a | 0); // 1000000, %a is converted to integer then string because the | operator can only take integers


So whenever you would:

  • Return a float
  • Call a function with a float
  • Assign a float to another variable without anything to make it look like a float (%b = %a; versus %b = %a + 0;)
  • Assign a float to a field on an object, since objects can only have string field values
  • Use a float in the bracketed part of a variable name
  • Anything else that forces string conversion

... just apply any integer operation (~ | & << >>) to it so that you do float -> integer -> string instead of float -> string, and you're good.

If you really do need a big number library for insanely large values, I believe Ipquarx has made one.

It's for a mining server where people stack hundreds of millions of cash, ints won't work here

Well, since you're using this for a mining server, you'll need a base-10 version of my bignum library, which I should be able to code without too much hassle. It won't support division (by anything but powers of 10) or modulus, though, so keep that in mind. You'll have addition, subtraction, and multiplication by any size number. I'll include a little documentation to help you use it.
« Last Edit: August 19, 2016, 03:42:08 PM by Ipquarx »

Okay, thank you very much!

Yeah, my code doesn't use much division (except for brick health buffs but I haven't seen brick health go so high yet)

Yeah, my code doesn't use much division (except for brick health buffs but I haven't seen brick health go so high yet)
Division is really just inverse multiplication. So instead of dividing the brick health by X, you can multiply someones pick level by X instead, although I don't know if you'd be alright with that workaround.

That's fine to me, I'll try to find another method if whatever I try to do doesn't work

How's progress going?