if and select vs. constants

Just starting out? Need help? Post your questions and find answers here.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

if and select vs. constants

Post by blueznl »

not entirely sure if it's a bug or a feature, but it definitely threw me of balance...

Code: Select all

#a = $8105
b.w = $8105
c.l = $8105

If b = #a
  Debug "ok1"
EndIf

Select b
  Case #a
    Debug "ok2"
EndSelect

If c = #a
  Debug "ok3"
EndIf

Select c
  Case #a
    Debug "ok4"
EndSelect
do 'if' and 'select' always turn a variable into a long before they do anything with it?
( 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... )
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

b.w is a negative (high bit on) ?
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

$8105 is a too big value for a Word variable, so you'll get a negative one.
(just as when you assign $FF to a byte, it'll become -1 )

So the word holds a different value then the others.
insert this, and you'll see:

Code: Select all

Debug #a
Debug b
Debug c
Your code does exactly what it is supposed to do.

Timo
quidquid Latine dictum sit altum videtur
User avatar
blueb
Addict
Addict
Posts: 1041
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Post by blueb »

Rather than assign the variable a value of -1, it would be nice if the compiler complained that the value was out of range.


blueb
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

on a binairy level, both values have the same bitmap presentation

(the large one in unsigned, the small one in signed)

the large value is not *changed* into a negative, but the variable is displayed and used by pb as a negative, ie. its storage in memory doesn't change

as the presentation in memory doesn't change, that means that, during a comparison, purebasic does something with it, probably converting it to a long on the fly

a.w = $8102
b.w = -32510
#c = $8102

if a = b now results in a true condition, as both are in unsigned hex $00008102 or in signed dec -32510

if a = #c is not resulting true because it's appearently converting the signed word -32510 into a signed long, while the constant fits in a signed long with converting

the assembly guru's amongst us migth confirm that it's actually comparing two longs instead of two words? or does it compare two words if both expressions left and right are words?

perhaps now i've made myself more clear :-)

though i dount that :lol:
( 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... )
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

I know about the binary level thing.

Here is what happens:
compare a to b:

you are comparing the value $8102, once interpreted as a signed word, which
is -32510, to the same, interpreted as a signed long, which is 33026.
It is obvious, that these values are different.

In memory, they are the same, but as they are interpreted differently,
they are different.

Of course, when in you second example, you set both to word type,
they are interpreted the same way, and thus are equal.


compare b to #c
b is the same as before: signed word, which is -32510
Now, you compare that to the immediate value 33026, which is not equal

Constants are put directly in the code, not in memory, so they are by
default the biggest thing that can be handled, which is long here.
And of course, everything is signed, so are they.

I hope that is clear now.

Timo
quidquid Latine dictum sit altum videtur
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

well, i think that last remark is the best explanation: constants become longs :-)

thanks, as i said, it threw me off balance
( 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... )
Kris_a
User
User
Posts: 92
Joined: Sun Feb 15, 2004 8:04 pm
Location: Manchester, UK

Post by Kris_a »

I think almost everything gets converted to a long at some point (mathematics, arguments in functions, comparison, etc). That's why they say it's best to just use a Long for everything (unless you're trying to cut back on memory)
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

That is, because the processor works in 32bit (long), at least for now...

Timo
quidquid Latine dictum sit altum videtur
Post Reply