Game Development Community

Functions for Large Number Math (No Scientific Notation)

by Dave Calabrese · 04/24/2009 (11:30 pm) · 3 comments

These functions will help when you need to work with large numbers since TorqueScript will convert very large numbers - the kinds you would typically have when working with scores, epoch time and other situations. Granted, there are only a few scenarios where you actually need to worry about large numbers, so having a few quick functions to handle those few scenarios should get the job done nicely.

Open up consoleFunctions.cc and drop this code in somewhere....

//----------------------------------------------------------------
// - Dave Calabrese / Gaslight Studios, Inc.
//----------------------------------------------------------------
// Console functions to work around TorqueScripts converting large numbers of Scientific Notation.
//  For the most part, TorqueScript normally does not need to work with very large numbers, so
//  there isn't a lot of point in reworking the entire engine to handle larger numbers.
//  That's where these functions come in to play - for those handful of times that you actually
//  do need to work with large numbers, just use these instead.
ConsoleFunction(getIntFromSciNot, int, 3, 3, "getIntFromSciNot(value,power)")
{
   float power = pow(10.0,dAtoi(argv[2]));
   int finalVal = atof(argv[1]) * power;

   return finalVal;
}

ConsoleFunction(addLargeValuesWithoutSciNot, int, 3, 3, "(valueA,valueB) Add valueA and valueB and maintain their proper form without converting to scientific notation.")
{
   int finalVal = dAtoi(argv[1]) + dAtoi(argv[2]);
   return finalVal;
}

ConsoleFunction(multLargeValuesWithoutSciNot, int, 3, 3, "(valueA,valueB) Multiply valueA and valueB and maintain their proper form without converting to scientific notation.")
{
   int finalVal = dAtoi(argv[1]) * dAtoi(argv[2]);
   return finalVal;
}

ConsoleFunction(divLargeValuesWithoutSciNot, int, 3, 3, "(valueA,valueB) Divide valueA and valueB and maintain their proper form without converting to scientific notation.")
{
   int finalVal = dAtoi(argv[1]) / dAtoi(argv[2]);
   return finalVal;
}

ConsoleFunction(subLargeValuesWithoutSciNot, int, 3, 3, "(valueA,valueB) Subtract valueB from valueA and maintain their proper form without converting to scientific notation.")
{
   int finalVal = dAtoi(argv[1]) - dAtoi(argv[2]);
   return finalVal;
}

ConsoleFunction(modLargeValuesWithoutSciNot, int, 3, 3, "(valueA,valueB) Get the modulus of valueA and valueB and maintain their proper form without converting to scientific notation.")
{
   int finalVal = dAtoi(argv[1]) % dAtoi(argv[2]);
   return finalVal;
}
//----------------------------------------------------------------

Then in TorqueScript, you can do any of the following....
addLargeValuesWithoutSciNot(52560423,8879652);
multLargeValuesWithoutSciNot(52560423,8879652);
divLargeValuesWithoutSciNot(52560423,8879652);
subLargeValuesWithoutSciNot(52560423,8879652);
getIntFromSciNot( getSubStr(%floatingEntry,0,5) , getSubStr(%powerEntry,7,3) );

(NOTE: The 'getIntFromSciNot()' function is a bit 'quick' and could use some extra functionality to allow you to just sent in a scientific notation based number instead of needing to chop it apart into its floating point and 'power of ten' components).

There ya go! A quick, solid way to work with large numbers in TorqueScript without making heavy modifications to the engine to force it to work with scientific notation on a global scale (which, again, really isn't needed since your situations are most likely going to be few and far between to need this - but you most likely WILL need it on occasion!).

Enjoy, feel free to tweak however. Lemme know what you think or of improvements you guys make!

- Dave Calabrese
Gaslight Studios, Inc.

#1
04/25/2009 (9:33 pm)
lol, synchronicity!
you might be interested in checking out this very recent resource. it provides very similar functions, but more interestingly, Wes MacDonald posted a codebase change which makes it so that the native torquescript math operators (+ - / *) all play nice with large integers.
#2
05/03/2009 (7:45 am)
Ah! The timing, lol... figures I searched through the forums and everything for awhile and somehow never stumbled upon that resource. I must have just missed it! Thanks for the heads-up, Orion!
#3
07/07/2009 (9:03 pm)
don't forget #include <math.h>