Jump to content

Calculation to a variable (TGE/Torque2d/T3D 3.10 / 4.0?)


Recommended Posts

In script I make something like that:

$bar = 1643588269 + 1;

==> 1643588224

and it returns a wrong value (i added code to display big numbers in TGE about 15 years ago so I dont talk about the +e09 stuff) but when I do

$foo = 1643588269 + 1 @ "";

==> 1643588270

it returns the correct value.

full test line would be:

$foo = 1643588269 + 1 @ "";$bar = 1643588269 + 1;echo($foo SPC $bar);


any ideas ? I tried to debug it with stop points at ConsoleGetType( TypeF32 ), void setFloatValue(F64 v), char *getFloatArg(F64 arg), U32 CompilerStringTable::addFloatString(F64 value) and void setFloatValue(F64 v) but no luck so far.  In script i did this typecast to string hack but there should be another solution.




Edited by ohmtal
Link to comment
Share on other sites


All calculations are saved as float (F32). I did a test change this to F64. Seams to run ok but breaks with my current running servers so it's not an option.

As workaround i do:

ConsoleFunction(addBig, const char*, 3, 3, "params: value1, value2") {

    U32 number1 = dAtoi(argv[1]);
    U32 number2 = dAtoi(argv[2]);
    U32 result;

    result = number1 + number2;

    char* returnBuffer = Con::getReturnBuffer(20);
    dSprintf(returnBuffer, 20, "%d", result);
    return returnBuffer;



Edited by ohmtal
Link to comment
Share on other sites

On 1/30/2022 at 7:03 PM, ohmtal said:


I think there might be a number of reasons why you are seeing strange values. It might have to do with the size of the values being stored in the given data type, but what isn't so clear is which data type is being used. Or at least this is true on the script side. I am not fully versed on the complete innerworkings of the engine, but I imagine there will be different templated internal data types depending on your usage of script variables. For instance $foo = 100, may be stored in a different internal type than $foo = 100.0, or $foo = "100". So $foo = 1643588269 + 1; might use a different internal type than $foo = 1643588269 + 1 @ "";

Just out of curiosity I'd be interested to know what happens when you try using smaller values like:

$bar = 164358 + 1;

Or when you give the engine a more explicit equation like:

$bar = 1643588269;

$foo = 1;

$result = $bar + $foo;

Also try placing decimals on the end of your numbers and see if there's any change. If at all possible, it is often best to steer clear of egregiously large numbers. It might "work" on your system, but might not on another. In theory standardized data types mitigate this but better safe than sorry.

Link to comment
Share on other sites

Smaller values works fine. When typecast to string with @ "" it's saved internally as string so it works. All other calculations are stored as float in F32  Dictionary::Entry::fval

in CMDGram.y the types are set to Float:

   | expr '+' expr
      { $$ = FloatBinaryExprNode::alloc($2, $1, $3); }
   | expr '-' expr
      { $$ = FloatBinaryExprNode::alloc($2, $1, $3); }
   | expr '*' expr
      { $$ = FloatBinaryExprNode::alloc($2, $1, $3); }
   | expr '/' expr
      { $$ = FloatBinaryExprNode::alloc($2, $1, $3); }

(code from TGE )

I leave it untouched at the moment. The problem came up when stored cooldowns did not work correctly since they use unixtimestamp (Platform::getTime()).   

Link to comment
Share on other sites

After reading CMDGram.y i got the idea it's also possible to force the variable to integer type using bitwise shifting (by 0) like this:

% $bla = 1643588269 + 1; $foo = 1643588269 + 1 @ "";$bar = 1643588269 + 1 | 0;echo($bla SPC $foo SPC $bar);
1643588224 1643588270 1643588270


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...