Ok... lesson learned... I tell you... just not thinking straight these days
Math syntax parsing issue Topic is solved
Re: Math syntax parsing issue
Last edited by andymbody on 07 Sep 2023, 21:40, edited 1 time in total.
-
- Posts: 4562
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Math syntax parsing issue
Info related to i * ++i: Sequence point (wiki)
- Noitalommi_2
- Posts: 393
- Joined: 16 Aug 2023, 10:58
Re: Math syntax parsing issue
I'm just wondering why not using "d++" instead ?teadrinker wrote: ↑07 Sep 2023, 19:46Basically, the problem lies here:The third operand in the examples above behaves as expected. @thqby's explanation is probably correct, but that doesn't mean it must be.Code: Select all
d := 1 MsgBox d * ++d ; expected 2, shows 4
Code: Select all
d := 1
msgbox d * d++ ; =2
Re: Math syntax parsing issue
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.
-
- Posts: 4562
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Math syntax parsing issue
@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?
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?
- Noitalommi_2
- Posts: 393
- Joined: 16 Aug 2023, 10:58
Re: Math syntax parsing issue
Would have thought it's "d * d + d"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?
But I don't know how it's handled either, I think I'll just wait and see.
-
- Posts: 4562
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Math syntax parsing issue
Then whyNoitalommi_2 wrote: ↑Would have thought it's "d * d + d"
Code: Select all
d := 1
MsgBox 1 * d++
-
- Posts: 4562
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Math syntax parsing issue
Another one example:
Try to predict, without executing the code, what this expression will equal.
Code: Select all
d := 1
MsgBox ++d + ++d
Re: Math syntax parsing issue
That is another good example of why it appears to be a bug, in my opinion.
Re: Math syntax parsing issue
That's why i used ++d. The docs explain this so far as i can tell:
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.
Come to think of it, this is the first time I've used ++ in an expression. Usually it's a single parameter.Var := X++ increments X but Var receives the value X had before it was incremented.
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.
« AHK Portable Installer » | « CallTipsForAll » | « TheArkive AHK v1 Scripts » | « TheArkive AHK v2 Scrpts » | « TheArkive on GitHub »
- Noitalommi_2
- Posts: 393
- Joined: 16 Aug 2023, 10:58
Re: Math syntax parsing issue
Hmmm. Maybee because there is no d var in the msg box to add +1 afterwards?teadrinker wrote: ↑07 Sep 2023, 22:48Then whyNoitalommi_2 wrote: ↑Would have thought it's "d * d + d"is 1?Code: Select all
d := 1 MsgBox 1 * d++
Re: Math syntax parsing issue
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
Last edited by andymbody on 08 Sep 2023, 01:28, edited 4 times in total.
Re: Math syntax parsing issue
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:
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
So ultimately it seems to stem from something about how assignment expressions are evaluated inside larger 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]
Code: Select all
i := 1
arr := [++i, ++i]
Re: Math syntax parsing issue
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])
Re: Math syntax parsing issue
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.
« AHK Portable Installer » | « CallTipsForAll » | « TheArkive AHK v1 Scripts » | « TheArkive AHK v2 Scrpts » | « TheArkive on GitHub »
Re: Math syntax parsing issue
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.
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.
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])
Re: Math syntax parsing issue
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
Re: Math syntax parsing issue
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?boiler wrote: ↑08 Sep 2023, 06:16It’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
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.