Author Topic: Weird Toque Logic Error?  (Read 1659 times)

Ok so. If I put this in the console:
%this = 0.7; if(%this){echo("True");}else{echo("False");}
It echos True.

And if I put this in the console:
%that = 1; if(%that){echo("True");}else{echo("False");}
It also echos True.

And if I put this in the console:
%that = 1; %this = 0.7; if(%that){if(%this){echo("True");}else{echo("False");}}
It echos True.

But if I put this in the console:
%that = 1; %this = 0.7; if(%that && %this){echo("True");}else{echo("False");}
It echos False.

Torque logic error or what?
« Last Edit: September 25, 2013, 03:10:27 PM by jes00 »

one of a few reasons that torques usage of variables is not up to par

How do you people find this stuff??


But why would you ever use a boolean check on anything other than a boolean (true, false, 1, 0) ??

But why would you ever use a boolean check on anything other than a boolean (true, false, 1, 0) ??

Exactly what he said. Regardless of if it "works" or not, if you want any non-zero value to result in true the check should be if(%this != 0)

This isn't actually a logic error so much as an edge case that you never see anywhere else because everywhere else would be saying "WTF MAN YOU CAN'T USE && ON TWO NON-BOOLEAN VALUES"

x && y is basically an x & y operation with the added caveat that it 'short-circuits' if x is false. Similarly, x || y is actually x | y but it short-circuits if x is true.

Since 1 & 0.7 is 0, the result is false even though both of the components are 'true' individually.



Exactly what he said. Regardless of if it "works" or not, if you want any non-zero value to result in true the check should be if(%this != 0)

That'd probably result in (%this != 0) != 0 because Torque.

Here's another funny error: http://forum.blockland.us/index.php?topic=233085.0

Badspot's explanation makes sense, though.

When I was testing this issue, it seems the low level version of the ! operator only takes into account the integer portion of a value once it has been stored in memory. Don't ask me why. You can see it by doing the following:

Code: [Select]
echo(0.7);
echo(!0.7);
echo(!!0.7);

$a = 0.7;
echo($a);
echo(!$a);
echo(!!$a);