Invert Value or however you want call it...

Share your advanced PureBasic knowledge/code with the community.
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post 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 :?:
User avatar
Deeem2031
Enthusiast
Enthusiast
Posts: 216
Joined: Sat Sep 20, 2003 3:57 pm
Location: Germany
Contact:

Post 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"
irc://irc.freenode.org/#purebasic
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post 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)
%1>>1+1*1/1-1!1|1&1<<$1=1
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post 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
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post 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.
%1>>1+1*1/1-1!1|1&1<<$1=1
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.)
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post 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:
%1>>1+1*1/1-1!1|1&1<<$1=1
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post 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
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

i like role model pictrues 8) especially the... euh... ones... :oops:
Last edited by blueznl on Mon Oct 10, 2005 9:08 pm, edited 1 time in total.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post by Bonne_den_kule »

So much discussion about easy and simple math... :D
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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 :(
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

i would like to see fred´s actual todo list :lol:
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
Post Reply