why you had 16, I don't understand. Typo?
--
Please read the posting guidelines at: http://groups.google.com/group/jBASE/web/Posting%20Guidelines
IMPORTANT: Type T24: at the start of the subject line for questions specific to Globus/T24
To post, send email to jB...@googlegroups.com
To unsubscribe, send email to jBASE-un...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/jBASE?hl=en
This is correct, reference this in C if you don’t believe me. Technically it is because the operator is unary right associative and is ‘pre’ increment. Hence, the instruction set generated is (roughly):
INC CNT in place ; CNT is now 4
INC CNT in place ; CNT is now 5
LD CNT ; 5
ADD CNT ; Add 5
And the answer is 10. I presume that you can derive the sequence for your second example from this.
Now contrast that with ‘post’ increment.
Jim
#include <stdio.h>
int
main(int cnt, char * argc[])
{
int c, c1;
c = 3;
printf("Expr is %d\n, c is %d\n\n", ++c + ++c, c);
c = 3;
printf("Expr is %d\n, c is %d\n\n", c++ + c++, c);
}
--
Look up the definition of the pre-increment and post-increment operators in C. They are right associative, which means that they operate to the right and as they are unary they have higher precedence than the binary “+” operator when they are the operands for the “+” operator. So the instruction sequence you get for your three piece combo with two binary “+” operations and three pre-increments is:
INC CNT ; CNT = 4
INC CNT ; CNT = 5
LD CNT ; 5
ADD CNT ; 10
INC CNT ; CNT=6
ADD CNT ; 16
; Et voila.
#include <stdio.h>
int
main(int cnt, char * argc[])
{
int c, c1;
c = 3;
printf("Expr is %d\n, c is %d\n\n", ++c + ++c, c);
c = 3;
printf("Expr is %d\n, c is %d\n\n", ++c + ++c + ++c, c);
c = 3;
printf("Expr is %d\n, c is %d\n\n", c++ + c++, c);
}
You are just confused over operator sequencing and precedence.
Jim
VK – if you are getting 18, then your system has a bug!! This sequence will not optimize to a multiplication because of the operator precedence requirements.
Jim me eld mucker,
All very well and good, but ……
On jBASE 3.x, the answer given is 9 (and then 15 for the second test). This is what Xze (??) was expecting and differs to what he saw on jBASE 5.x , answers of 10 and 16.
So no matter what your argument, the fact we get different answers on different jBASE releases shows that something is wrong. As you were responsible for the jBASE 3.x compiler and not for the jBASE 5.x compiler, I would obviously expect the jBASE 3.x answer to be correct ;-)
You then talk about “reference this in C if you don’t believe me”.
Now, not as though I didn’t believe you or anything, but writing a simple
C program on Suse 11.2 gives this
main()
{
int cnt , res ;
cnt = 3 ;
res = ++cnt + ++cnt ;
printf("cnt = %d\n",cnt);
}
cnt = 5
So that doesn’t match up with either of the jBASE releases or what you had said about C.
So there is obviously some behaviour that is hard to pin down with these pre and post increments.
I am sure I am missing something here, and await your demolishment of this email to prove you right again ! ;-)
Greg
Greg,
Thank you for your proof of Fermat’s last theorem, your mistake is at line 6.
I have been trying to teach you C for so long now that I cannot remember how long it is, but if this is an example of how much you have learned, then perhaps I spent about 20 minutes on your lessons? Run the program I attached and not your incorrect program. Mine confirms that cnt is 5 and that therefore the expression is 10, which your C program is merely confirming… as well as confirming that one should not try to write C at 9PM on a Sunday after a few beers and a curry… unless you are me.
If a prior version of jBASE did not return 10 in this instance then it was incorrect. In fact, I think I remember fixing this bug in 4.x (and specifically only at the major point release)… or maybe I don’t. Perhaps you remember me doing this… nah, it was more than 20 minutes ago ;-) (c.f. The Invisible Gorilla)
Anyway, your program is evaluating the expression and storing it in the variable ‘res’, but then your printf statement is outputting the value of cnt (I think you are missing a letter perhaps? =:?o), which as cnt is has been incremented twice by then, will, as you acidulously demonstrate, have the value 5, thus proving that C thinks that 3 +1 +1 is 5.
So, you have fallen for the same issue only worse, in that the ++var will always happen before the binary operator + and so this will be 5 + 5 = 10. However your mistake is even worse because you are not even printing out the correct variable. If you are having trouble typing in the program that I attached to my email in the first place, then please let me know, so I can show you how to type in October when I am back in London. I was going to visit with you of course, but I am not sure if I should be associating with junior C programmers ;-)
Now, it could well be that the original parser at some point in 3.x did not have the correct precedence on the pre_increment operator and so it gives the wrong answer. However, the answer that the OP is getting from the latest jBASE is the correct one.
In future my eld padwan, you should concentrate on handing out the res due to your old master, otherwise you risk looking like a
As I presume that pointing out that your C program is utter crap and merely verifies my much cleverer program, I take it that this is sufficient demolition of your argument and that you will now be buying the second and third rounds as well as the first, as well of course being…
Yours respectfully, etc etc,
Ben Franklin.
Charlie Noah
The views and opinions expressed herein are my own (Charlie Noah) and do not necessarily reflect the views, positions or policies of any of my former, current or future employers, employees, clients, friends, enemies or anyone else who might take exception to them.
In general, the best way to handle such issues is to avoid them, be more
explicit with parenthesis and break down the expression. In fact there is a
more important reason why you should do that and this is that 'clever'
expressions such as this are beyond the ability of most to just read the
code and know exactly what it is doing. Hence, for the sake of
maintainability, we should never be 'clever' unless it is the only way. The
optimizers will take care of almost everything these days. Besides which,
the usual problem with a program is algorithmic, which neither a clever line
of code, nor an optimizer (at least, not as yet) can fix.
Jim