It is currently Tue Jan 26, 2021 6:21 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: if and select vs. constants
PostPosted: Wed Mar 10, 2004 12:33 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Sat May 17, 2003 11:31 am
Posts: 6074
not entirely sure if it's a bug or a feature, but it definitely threw me of balance...

Code:
#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?

_________________
( PB5.xx Win10 x64 Asrock AB350 Pro4 Ryzen 1600X 32GB RAM Evo 840 GTX1060 )
( The path to enlightenment and the PureBasic Survival Guide right here... )


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Mar 10, 2004 12:46 pm 
Offline
Moderator
Moderator

Joined: Sat Dec 27, 2003 3:55 am
Posts: 3321
Location: Great Southern Land
b.w is a negative (high bit on) ?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Mar 10, 2004 2:14 pm 
Offline
PureBasic Team
PureBasic Team
User avatar

Joined: Fri Apr 25, 2003 5:21 pm
Posts: 5818
Location: Germany
$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:
Debug #a
Debug b
Debug c


Your code does exactly what it is supposed to do.

Timo

_________________
quidquid Latine dictum sit altum videtur


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Mar 10, 2004 4:04 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Apr 26, 2003 2:15 pm
Posts: 901
Location: Cuernavaca, Mexico
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Mar 10, 2004 5:30 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Sat May 17, 2003 11:31 am
Posts: 6074
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:

_________________
( PB5.xx Win10 x64 Asrock AB350 Pro4 Ryzen 1600X 32GB RAM Evo 840 GTX1060 )
( The path to enlightenment and the PureBasic Survival Guide right here... )


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Mar 10, 2004 6:01 pm 
Offline
PureBasic Team
PureBasic Team
User avatar

Joined: Fri Apr 25, 2003 5:21 pm
Posts: 5818
Location: Germany
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Mar 11, 2004 12:55 am 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Sat May 17, 2003 11:31 am
Posts: 6074
well, i think that last remark is the best explanation: constants become longs :-)

thanks, as i said, it threw me off balance

_________________
( PB5.xx Win10 x64 Asrock AB350 Pro4 Ryzen 1600X 32GB RAM Evo 840 GTX1060 )
( The path to enlightenment and the PureBasic Survival Guide right here... )


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Mar 11, 2004 12:42 pm 
Offline
User
User

Joined: Sun Feb 15, 2004 8:04 pm
Posts: 92
Location: Manchester, UK
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)


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Mar 11, 2004 3:48 pm 
Offline
PureBasic Team
PureBasic Team
User avatar

Joined: Fri Apr 25, 2003 5:21 pm
Posts: 5818
Location: Germany
That is, because the processor works in 32bit (long), at least for now...

Timo

_________________
quidquid Latine dictum sit altum videtur


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Steving and 58 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye