BUG REPORT: Order of numbers alters sum

Started by Kairus, Sun 15/02/2004 18:47:14

Previous topic - Next topic

Kairus

I've been for a good while trying to understand why my character was moving differently from what I'd scripted when I came across something really odd. I tried to isolate the problem and got this:
Open you favorite game.
Choose your favorite label.
Write this in the script:

 string data;
 StrFormat (data, "%d %d %d",3+4-5,3-5+4,-5+3+4);
 SetLabelText (0,0,data);

And the results....

2 -6 -12

Impressed? Well, I was taught since I was very little that the order of the numbers didn't alter the result of the sum (except in infinite sums, which is not the case). I think the compiler is associating the operations like this:

3 + 4 - 5 = 2 (correct)
3 - (5 + 4) = -6 (wrong)
-(5 + 3 + 4) = -12 (even worse)

Wouldn't it be wise to correct it so it's mathematically coherent for the next version of AGS?
Download Garfield today!

DOWNLOADINFOWEBSITE

Kweepa

This is something CJ has commented on before.
Basically he said he wasn't going to change it because existing scripting would be broken.
I don't agree with the decision but whaddaya gonna do?
The solution, anyway, is to always use brackets to force left to right evaluation.
Still waiting for Purity of the Surf II

Kairus

It could be a feature that could be changed in the main game settings, so as to make people used to the correct system. Otherwise this error will be carried forever, and it's no good.

Please, if CJ reads this, try to change it for the next version. Thanks.
Download Garfield today!

DOWNLOADINFOWEBSITE

Gilbert

Heh I suggest not changing anything, unless there's a complete overhaul in scripting system in soem future version.
Just use brackets whereever possible.

SMF spam blocked by CleanTalk