Math syntax parsing issue Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
andymbody
Posts: 1034
Joined: 02 Jul 2017, 23:47

Re: Math syntax parsing issue

Post by andymbody » 07 Sep 2023, 21:38

teadrinker wrote:
07 Sep 2023, 21:34
@andymbody

Code: Select all

MsgBox 1 / 1
Ok... :lol: lesson learned... :oops: I tell you... just not thinking straight these days
Last edited by andymbody on 07 Sep 2023, 21:40, edited 1 time in total.

teadrinker
Posts: 4562
Joined: 29 Mar 2015, 09:41
Contact:

Re: Math syntax parsing issue

Post by teadrinker » 07 Sep 2023, 21:40

Info related to i * ++i: Sequence point (wiki)

User avatar
Noitalommi_2
Posts: 393
Joined: 16 Aug 2023, 10:58

Re: Math syntax parsing issue

Post by Noitalommi_2 » 07 Sep 2023, 22:16

teadrinker wrote:
07 Sep 2023, 19:46
Basically, the problem lies here:

Code: Select all

d := 1
MsgBox d * ++d ; expected 2, shows 4
The third operand in the examples above behaves as expected. @thqby's explanation is probably correct, but that doesn't mean it must be.
I'm just wondering why not using "d++" instead ?

Code: Select all

d := 1
msgbox d * d++ ; =2

User avatar
andymbody
Posts: 1034
Joined: 02 Jul 2017, 23:47

Re: Math syntax parsing issue

Post by andymbody » 07 Sep 2023, 22:25

Noitalommi_2 wrote:
07 Sep 2023, 22:16
I'm just wondering why not using "d++" instead ?
The OP was asking why ++d was causing unexpected results, not for advice about whether it should be used. This sparked the replies on the topic of a possible bug. So technically the thread should probably be moved to a bug reporting section?
Last edited by andymbody on 07 Sep 2023, 22:27, edited 1 time in total.

teadrinker
Posts: 4562
Joined: 29 Mar 2015, 09:41
Contact:

Re: Math syntax parsing issue

Post by teadrinker » 07 Sep 2023, 22:27

@Noitalommi_2
The task is not to fit the expression to the result, but to understand why the result is the way it is. The result of the expression you gave is not intuitive either. After all, in the case of postincrement, the value of the variable is taken first, the expression with it is evaluated, and only after that the increment is performed. So why is the result 2? :lol:

User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Math syntax parsing issue

Post by boiler » 07 Sep 2023, 22:34

andymbody wrote: This sparked the replies on the topic of a possible bug. So technically the thread should probably be moved to a bug reporting section?
We could move it, but since it’s in a gray area and lexikos has already been pinged, I’m inclined to leave it here for now and see what he says.

User avatar
Noitalommi_2
Posts: 393
Joined: 16 Aug 2023, 10:58

Re: Math syntax parsing issue

Post by Noitalommi_2 » 07 Sep 2023, 22:43

teadrinker wrote:
07 Sep 2023, 22:27
@Noitalommi_2
The task is not to fit the expression to the result, but to understand why the result is the way it is. The result of the expression you gave is not intuitive either. After all, in the case of postincrement, the value of the variable is taken first, the expression with it is evaluated, and only after that the increment is performed. So why is the result 2? :lol:
Would have thought it's "d * d + d"
But I don't know how it's handled either, I think I'll just wait and see.

teadrinker
Posts: 4562
Joined: 29 Mar 2015, 09:41
Contact:

Re: Math syntax parsing issue

Post by teadrinker » 07 Sep 2023, 22:48

Noitalommi_2 wrote: Would have thought it's "d * d + d"
Then why

Code: Select all

d := 1
MsgBox 1 * d++
is 1?

teadrinker
Posts: 4562
Joined: 29 Mar 2015, 09:41
Contact:

Re: Math syntax parsing issue

Post by teadrinker » 07 Sep 2023, 22:56

Another one example:

Code: Select all

d := 1
MsgBox ++d + ++d
Try to predict, without executing the code, what this expression will equal.

User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Math syntax parsing issue

Post by boiler » 07 Sep 2023, 23:03

That is another good example of why it appears to be a bug, in my opinion.

User avatar
TheArkive
Posts: 1032
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: Math syntax parsing issue

Post by TheArkive » 07 Sep 2023, 23:05

That's why i used ++d. The docs explain this so far as i can tell:
Var := X++ increments X but Var receives the value X had before it was incremented.
Come to think of it, this is the first time I've used ++ in an expression. Usually it's a single parameter.

thqby-s description certainly makes sense. And teadrinker's post on "sequence points" sheds some light too.

Thanks for all the back and forth guys. I learned a lot.

EDIT: Still very curious what lexikos will say.

User avatar
Noitalommi_2
Posts: 393
Joined: 16 Aug 2023, 10:58

Re: Math syntax parsing issue

Post by Noitalommi_2 » 07 Sep 2023, 23:38

teadrinker wrote:
07 Sep 2023, 22:48
Noitalommi_2 wrote: Would have thought it's "d * d + d"
Then why

Code: Select all

d := 1
MsgBox 1 * d++
is 1?
Hmmm. Maybee because there is no d var in the msg box to add +1 afterwards?

User avatar
andymbody
Posts: 1034
Joined: 02 Jul 2017, 23:47

Re: Math syntax parsing issue

Post by andymbody » 08 Sep 2023, 00:33

Noitalommi_2 wrote:
07 Sep 2023, 23:38
d := 1
msgbox d * d++ ; =2
you may already know this... but... just in case...

As far as I know...

++c - operations to the immediate left see the NEW value, which can be used in the operation (rather than using the original unaltered value)
c++ - operations to the immediate left see the ORIGINAL (current/unaltered) value, then the value of c is incremented for operations to the right

an analogy of the difference between these two operators might look like this...

c=1 - A train engine has 1 car attached
++c - add another car to the train (two now), then pull both cars to the destination (operation completed using new value)
c++ - pull one car (the original), then once you arrive at the destination (operation complete using original value), add a second car

Correct me if I am wrong...?

in the example above its the same as:

Code: Select all

msgbox d*d	; 1
d=d+1		; now 2
Andy
Last edited by andymbody on 08 Sep 2023, 01:28, edited 4 times in total.

bonobo
Posts: 81
Joined: 03 Sep 2023, 20:13

Re: Math syntax parsing issue

Post by bonobo » 08 Sep 2023, 01:19

I suspect the root cause is due to a bug (?) with how the assignment expressions works inside expressions.

Consider that `(i++)++` throws an error but `++(++i)` doesn't. This is because `++i` evaluates to the variable i itself whereas `i++` evaluates to a primitive value.

Basically, when you have `++i` inside an expression, it's exactly as if `i:=i+1` occurred in its place.

Now consider this code:

Code: Select all

i := 1
arr := [i:=i+1, i:=i+1]
Intuitively you may predict `arr` to be [2,3], but in fact it's [3,3]. This corresponds exactly to the counterintuitiveness of the following code

Code: Select all

i := 1
arr := [++i, ++i]
So ultimately it seems to stem from something about how assignment expressions are evaluated inside larger expressions.

User avatar
andymbody
Posts: 1034
Joined: 02 Jul 2017, 23:47

Re: Math syntax parsing issue

Post by andymbody » 08 Sep 2023, 01:34

bonobo wrote:
08 Sep 2023, 01:19
arr := [i:=i+1, i:=i+1]
Clarification...
Would this not be:
arr := [i:=1+1, i:=2+1] ; therefore [i:=2, i:=3] ?

bonobo
Posts: 81
Joined: 03 Sep 2023, 20:13

Re: Math syntax parsing issue

Post by bonobo » 08 Sep 2023, 01:43

andymbody wrote:
08 Sep 2023, 01:34
bonobo wrote:
08 Sep 2023, 01:19
arr := [i:=i+1, i:=i+1]
Clarification...
Would this not be:
arr := [i:=1+1, i:=2+1] ; therefore [i:=2, i:=3] ?
That's what I initially expected also, but the actual result is [3,3].

Code: Select all

i := 1
arr := [i:=i+1, i:=i+1]
MsgBox(arr[1])
MsgBox(arr[2])

User avatar
TheArkive
Posts: 1032
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: Math syntax parsing issue

Post by TheArkive » 08 Sep 2023, 01:51

Code: Select all

i := 1
arr := [++i, ++i] ; = [3, 3]

That's interesting. Didn't expect that one. I would have thought the comma would have split up the expression, but that seems to not be the case. It's not without logic though. Thinking back to thqby's explanation, it makes sense as to why, given that all of this is happening in the same expression.

EDIT:

Code: Select all

i := 1
arr := [i:=i+1, i:=i+1] ; = [3,3]

Woah, didn't expect that either.

User avatar
andymbody
Posts: 1034
Joined: 02 Jul 2017, 23:47

Re: Math syntax parsing issue

Post by andymbody » 08 Sep 2023, 05:37

What do you expect here? It's not normal for sure. And as @teadrinker pointed out, it doesn't seem to happen in other languages.

It seems to have something to do with the COLON character. And each of the elements that has a colon returns the same value (the last two elements with colons added up). It doesn't seem to have anything to do with math, as element 6 is the only one with math. I'm still experimenting to see.

:crazy:

Code: Select all

i := 100000
arr := [i:=1000, -2000, i:=3, i=4, i:=5, i:=i+600, 7]
MsgBox(arr[1])
MsgBox(arr[2])
MsgBox(arr[3])
MsgBox(arr[4])
MsgBox(arr[5])
MsgBox(arr[6])
MsgBox(arr[7])

User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Math syntax parsing issue

Post by boiler » 08 Sep 2023, 06:16

It’s not unique to the colon. As teadrinker asked earlier, try to predict the following result, then run it and see the actual result (or view the spoiler).

Code: Select all

d := 1
MsgBox ++d + ++d
Spoiler

User avatar
andymbody
Posts: 1034
Joined: 02 Jul 2017, 23:47

Re: Math syntax parsing issue

Post by andymbody » 08 Sep 2023, 07:26

boiler wrote:
08 Sep 2023, 06:16
It’s not unique to the colon. As teadrinker asked earlier, try to predict the following result, then run it and see the actual result (or view the spoiler).

Code: Select all

d := 1
MsgBox ++d + ++d
Yes, as the example I posted had this, and a possible clue to what may be happening? But as @bonobo pointed out, when substituting i:=i+1 for ++d a similar issue emerges. I can't imagine that the internal C++ language is causing this behavior. I would think that would have been documented a long time ago. @teadrinker Did you happen to test C++ along with the other languages?

I was simply pointing out that in that example, the colon char seems to provide a clue. Not that the colon char itself is suspect.
Last edited by andymbody on 08 Sep 2023, 07:34, edited 1 time in total.

Post Reply

Return to “Ask for Help (v2)”