F in Math Calculation
by Gellyware · in Torque Game Builder · 01/20/2007 (10:09 pm) · 8 replies
Ok, either I deserve the F for calculation error || TGB does.
Here is the problem:
Note the comment inside the if statement. The result should be -1, therefor the IF statement should be executed. But it is not. It echos that result is greater than zero.
Here is the problem:
%rot1 = 1;
%rot2 = -2;
%result = (( %rot1 + %rot2) % 4); //should be -1
if(%result < 0)
{
//guess what, %result SHOULD = -1, so it should be less than zero
//however TGB is passing up this IF statement
echo("RESULT < 0");
%r = 4 + %result;
%result = %r;
}
else
echo("Result is GREATER than Zero");Note the comment inside the if statement. The result should be -1, therefor the IF statement should be executed. But it is not. It echos that result is greater than zero.
About the author
#2
The output is surprising!!!
Notice how the numerical comparisons for less than zero and equal to negative one both failed, but the string comparison to "-1" succeeded. All I can say is, wow...
01/21/2007 (2:21 am)
That is amazingly insane. I've stepped through your original code and it seems the mod call is somehow turning your value into a string. I added these lines just before the original if statement:error("result = " @ %result);
error("%result < 0: " @ (%result < 0));
error("%result == -1: " @ (%result == -1));
error("%result $= \"-1\": " @ (%result $= "-1"));The output is surprising!!!
result = -1 %result < 0: 0 %result == -1: 0 %result $= "-1": 1
Notice how the numerical comparisons for less than zero and equal to negative one both failed, but the string comparison to "-1" succeeded. All I can say is, wow...
#3
Seems like the mod operator is making vars unrecognizable as ints. Crazy man.
01/21/2007 (2:33 am)
Then I tried this little test:$p = -1; // $p is -1 error($p); // prints -1 error($p == -1); // true $p = $p % 4; error($p); // prints -1 error($p == -1); // false! <--------------------------------- error($p $= "-1"); // true!
Seems like the mod operator is making vars unrecognizable as ints. Crazy man.
#4
I thought it was something with the mod operator but couldn't pinpoint exactly what was happening.
Something that definately needs a fixing :)
01/21/2007 (3:03 am)
Thanks for the reply Ben, Nice thorough troubleshooting.I thought it was something with the mod operator but couldn't pinpoint exactly what was happening.
Something that definately needs a fixing :)
#5
01/21/2007 (3:06 am)
Also, one thing that I've noticed is that it only happens on the negative modulus.
#6
Math modulus is defined as 0 to n-1, there are no negatives (-1 would be n-1)
If I'm not wrong there was a thread one or two weaks ago where exactly this was discussed, because C++ programmers have no chance not running into this "wall" if they use mod
01/21/2007 (5:26 pm)
Perhaps it tries to use mathematical modulus instead of coder modulus which is out of definition simply wrong.Math modulus is defined as 0 to n-1, there are no negatives (-1 would be n-1)
If I'm not wrong there was a thread one or two weaks ago where exactly this was discussed, because C++ programmers have no chance not running into this "wall" if they use mod
#7
I actually have tried the exact formula in Blitzmax (thats where I designed the formula due to speed of figuring it out) and it works perfect every time, so it's a TGB issue somewhere.
01/21/2007 (8:36 pm)
That could be what the problem is... I haven't ever had a need for a negative modulus before... but now I do since I'm dealing with left and right rotations and need to determine out of 4 choices which is correct after n rotations. Modulus is the only method that I could think of that worked.I actually have tried the exact formula in Blitzmax (thats where I designed the formula due to speed of figuring it out) and it works perfect every time, so it's a TGB issue somewhere.
#8
01/22/2007 (9:33 am)
If you put aside the argument of modulus supposed to be from 0 to n-1 then TGB is figuring out the modulus "correctly" in that it is returning a number from -(n-1) to (n-1). The problem is the returned results from -(n-1) to -1 are returned as a string and not an int.
Torque Owner Gellyware
%rot1 = 1; %rot2 = -2; %result = (%rot1 + %rot2); if(%result < 0) %result = 4 + (%result % 4); else %result = %result % 4; if(%result == 4) //possible for result to = 4 w/ the right %rot1 and %rot2 values %result = 0;In this version, if the result is less than zero then the formula is applied properly. Is it order of operation? I dont get it.