Adventure Game Studio Official

User reported issues for officially released versions of AGS

Bug Issue: Option "Left-to-right operator precedence" is ignored

Pages: [1]

fernewelten

  • AGSer
  • Posts: 64
« on: 23 Jun 2018, 00:39 »
Hello,

  • From startup, create a new project Type "default"
  • Note that in tab "General Settings", "Left-to-right operator precedence" is set to "True" as a default. Leave it that way.
  • In the project explorer, open "Rooms", "1:",  doubleclick "Edit room".
  • Hit F4 (or select the event grid with the flash button).
  • Put cursor in the field "Enters room after fade-in" and click the "..." button.
  • Enter the following code in the room script file:

Code: Adventure Game Studio
  1. function room_AfterFadeIn()
  2. {
  3.     DrawingSurface *ds = Room.GetDrawingSurfaceForBackground();
  4.     ds.DrawingColor = Game.GetColorFromRGB(255, 255, 255);
  5.     int i = 97;
  6.     ds.DrawString(10, 100, eFontSpeech, "130 + i * i / 110 * 3 = %d", 130 + i * i / 110 * 3);
  7.     ds.Release();
  8. }

  • Compile-and-run by hitting F5
  • Observe the output: 130 + i * i / 110 * 3 = 158.
  • Note that this calculation has been done right-to-left, i.e. 130 + (i * (i / (110 * 3))), contrary to the option "Left-to-right operator precedence".

The correct result should have been 130 + (((i * i) / 110) * 3) = 258. This is what I was expecting.

AGS Help gives the following version information:
Quote
AGS Editor .NET (Build 3.4.1.12)
v3.4.1, January 2018
Copyright © 2006-2011 Chris Jones and 2011-2018 others.
Scintilla (c) 1998-2003 Neil Hodgson, all rights reserved
DockPanel Suite (c) 2007 Weifen Luo
See the DOCS folder for copyrights of used libraries.
System: Windows 10
Editor plugins loaded:
* ags.plugin.notes.dll

Crimson Wizard

  • AGSer
  • Posts: 8,594
« Reply #1 on: 23 Jun 2018, 01:30 »
Hmm, yes, this is bizzare.
Either option is not set in the compiler properly, or there is simply a bug in compiler itself.

I would suspect the latter, because switching option to "Right-to-left" actually changes the logic in calculations, breaking existing game.

EDIT2: I can confirm, that in case of +/- operators the setting works. For example
Code: Adventure Game Studio
  1. int a = 5 - 3 - 2;
  2. Display("%d", a);
  3.  
Left-to-right ON: result is 0
Left-to-right OFF: result is 4.
  • Status changed from New to Confirmed
  • Priority changed from 2 to 3
« Last Edit: 23 Jun 2018, 21:15 by Crimson Wizard »

Radiant

  • AGSer
  • Posts: 5,217
  • Return once more to the Two Kingdoms!
« Reply #2 on: 23 Jun 2018, 21:35 »
Note that the name of this option is a bit cryptic... left-to-right was intended to match the C/C++ standard, whereas right-to-left was intended to match AGS legacy code from 2.6 and earlier.

Crimson Wizard

  • AGSer
  • Posts: 8,594
« Reply #3 on: 23 Jun 2018, 04:00 »
It suddenly struck me that the problem may be not in right-to-left precendence, but in operator precendence: if multiplication has a precedence over division.

So I did following test:

Code: Adventure Game Studio
  1. int a = 1000 / 2 * 5 / 10;
  2. Display("%d", a);
  3.  

With "left-to-right" precedence the result is : 10
With "right-to-left" precedence the result is : 1000

This means that AGS treats above as:
Code: Adventure Game Studio
  1. int a = 1000 / (2 * 5) / 10;
  2.  

In other words, left-to-right precedence is working, but multiplication has higher priority than division.


Same is true in your case:

 130 + i * i / 110 * 3, where i = 97
is treated as
  130 + (97 * 97) / (110 * 3) = 130 + 9409 / 330 = 130 + 28 = 158
« Last Edit: 23 Jun 2018, 04:05 by Crimson Wizard »

Issue updated by Crimson Wizard
« Reply #4 on: 23 Jun 2018, 01:39 »
  • Category changed from Text Parser to Script

Issue updated by Crimson Wizard
« Reply #5 on: 23 Jun 2018, 21:16 »
  • Priority changed from 3 to 2

Pages: [1]

Issue Details

  • Reported
    23 Jun 2018, 00:39
  • Updated
    23 Jun 2018, 21:35
  • View Status
    Public
  • Type
    Bug
  • Status
    Confirmed
  • Priority
    Normal
  • Version
    AGS 3.4.1
  • Fixed in
    (none)
  • Assigned to
    (none)
  • Category
    Script

Tags



Powered by: SMF Project Tools 0.5.4 © Niko Pahajoki 2007-2011