Page 3 of 3

Posted: Sat Oct 08, 2005 11:52 am
by Dr. Dri
Trond wrote:If you want to write really obfuscated code you can use this:

Code: Select all

a-a-a
:wink:
is that normal ?
if i do the same with a C compiler (a -= a - a) a doesn't change.
it's like a -= 0

Dri :?:

Posted: Sat Oct 08, 2005 12:11 pm
by Deeem2031
Dr. Dri wrote:
Trond wrote:If you want to write really obfuscated code you can use this:

Code: Select all

a-a-a
:wink:
is that normal ?
if i do the same with a C compiler (a -= a - a) a doesn't change.
it's like a -= 0

Dri :?:
Its normal, because the PBCompiler replace "a-" with "a=a-" -> "a=a-a-a" and this is like "a=-a"

Posted: Sat Oct 08, 2005 12:37 pm
by Froggerprogger
Hmm. In C:

Code: Select all

a -= a-a
is interpreted as subtracting the 'whole' right side of the expression.

Code: Select all

a = a - (a-a)
so

Code: Select all

a = a - a + a = a
in opposite to PB:

Code: Select all

a-a-a
is simply interpretated as

Code: Select all

a = a-a-a = 0-a = -a
So there's really a sematic difference between:

Code: Select all

a -= a-a (C)
and

Code: Select all

a-a-a (PB)
Never thought about, but good to know.
In PB you'll get the C-behaviour by

Code: Select all

a-(a-a)

Posted: Sat Oct 08, 2005 1:01 pm
by Dr. Dri
if you take a look at the help file, you'll see
variable - expression ;...

so if expression is a - a it shouldn't behave this way

Dri

Posted: Sat Oct 08, 2005 1:37 pm
by Froggerprogger
Hey, you're right!
The helpfile is inconsistent to the implementation there.
It says (in translation from German):
variable - expression:
The value of the expression is subtracted from variable
That's not true!

Code: Select all

a = 10
debug a-1-1
gives 8, not 10, though the value of the 'expression' is 1-1 = 0

It would be nice to update the docs there, but not change the behaviour.
I find it intuitive and would expect, that the expression a-1-1 gives 8 here, as well I would expect a -= 1-1 to result in 10.

Posted: Sat Oct 08, 2005 3:04 pm
by Trond
Froggerprogger wrote:Hey, you're right!
The helpfile is inconsistent to the implementation there.
It says (in translation from German):
variable - expression:
The value of the expression is subtracted from variable
That's not true!

Code: Select all

a = 10
debug a-1-1
gives 8, not 10, though the value of the 'expression' is 1-1 = 0

It would be nice to update the docs there, but not change the behaviour.
I find it intuitive and would expect, that the expression a-1-1 gives 8 here, as well I would expect a -= 1-1 to result in 10.
Everyone seems a bit confused. I can assure that everything is normal and working fine. (Well, actually the manual is a small bit inconsistent with itself.)

Code: Select all

a-a
a-a = 0. Since there is not assignment operator, the result is put into the first of the variable on the left side. The result is that a equals 0.

Code: Select all

a-a-a
Let's say a is 5. That gives us this expression: 5-5-5. Obviously, the result should be -5. Since there is no assignment operator, the result is put into the first of the variables on the left side. A therefore equals -5 after execution.

Code: Select all

a-1-1
Let's say a is 10. That gives the expression 10-1-1 (=8). Since there is no assignment operator the result is put into a.

Then for the "strange" manual. It is not exactly wrong, that depends on how you expect the compiler to handle expressions. You say that 1-1 is 0. That's one way of looking at it. On the other hand, we can say that it is implicit that the expression on the RHS is negative since it follows a minus (and ever number following a single minus is negative) and since the first minus and the second minus has the same precedence level and gets calculated from left to right it is not wrong to assume that the actual expression is -1-1.

But if it was me who wrote the manual I would have rephrased it because as it is now it is more natural to interpret it different from how the compiler interprets it. (Pun unintended.)

Posted: Sat Oct 08, 2005 4:55 pm
by Froggerprogger
@Trond
I too think, that a-a-a has the correct and expected behaviour, though it is different from a -= a-a

But the manual is 'exactly wrong' in this point. Or at least usually an information in a programming-language-doc given in such a formal way as

Code: Select all

variable - expression
has to be interpretated in a strict formal syntactic way.
It's like a definition made in BNF:
variable and expression are non-terminals, while '-' is a terminal symbol.
so in a-a-a there the is:
variable == a,
'-' == '-'
expression == a-a

So the value of the expression is 0.

For sure this is not really worth the lines I write on this topic, but it is one of the small nice inconsistencies where somebody can be happy to find them and talk about. :wink:

Posted: Sat Oct 08, 2005 5:38 pm
by Trond
It's not that I don't see that it's clearly an error in the manual. It's just that if I say that something minor like that is wrong Dare2 pops up and calls me "Role model for Anal Retentives" so that's why I was a bit modest.

Posted: Sat Oct 08, 2005 9:21 pm
by Dr. Dri
Trond wrote:

Code: Select all

a-a-a
Let's say a is 5. That gives us this expression: 5-5-5. Obviously, the result should be -5. Since there is no assignment operator, the result is put into the first of the variables on the left side. A therefore equals -5 after execution.
you point the problem... if "-" is the first operator, then it's also an assignment operator. this is the same behavior with others operators (- * / % & |...)

Dri

Posted: Mon Oct 10, 2005 8:34 pm
by Psychophanta
Dr. Dri wrote:asm output code (/commented) :shock:

Code: Select all

; a = -a
  MOV    ebx,dword [v_a]
  NEG    ebx
  MOV    dword [v_a],ebx
working "inlined" asm code :!:

Code: Select all

a = 1
!NEG [v_a]
Debug a
I think "NEG a" is faster than using ebx but maybe i'm wrong... It may be optimized as "a + 1" becomes "INC a".

Dri :idea:
Fred wrote:True, it can be easily optimized, i put it on the todo list
Heeeyy...
Don't for get this too:

Code: Select all

a*-1
and

Code: Select all

a/-1
That should be converted to !NEG [v_a] too
And of course !FCHS if it is a float

Posted: Mon Oct 10, 2005 9:06 pm
by blueznl
i like role model pictrues 8) especially the... euh... ones... :oops:

Posted: Mon Oct 10, 2005 9:06 pm
by Bonne_den_kule
So much discussion about easy and simple math... :D

Posted: Sun Aug 06, 2006 11:47 am
by Psychophanta
Dr. Dri wrote:asm output code (/commented) :shock:

Code: Select all

; a = -a
  MOV    ebx,dword [v_a]
  NEG    ebx
  MOV    dword [v_a],ebx
working "inlined" asm code :!:

Code: Select all

a = 1
!NEG [v_a]
Debug a
I think "NEG a" is faster than using ebx but maybe i'm wrong... It may be optimized as "a + 1" becomes "INC a".

Dri :idea:
Fred wrote:True, it can be easily optimized, i put it on the todo list
Still not done in PB4.0 :(

Posted: Sun Aug 06, 2006 7:27 pm
by va!n
i would like to see fred“s actual todo list :lol: