It is currently Fri Nov 22, 2019 5:00 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: PB 5.11b1 bug with short value assignment
PostPosted: Sun Mar 10, 2013 11:02 am 
Offline
User
User

Joined: Sun Sep 16, 2012 12:13 pm
Posts: 65
Hello Fred,

lst(Random(255))+1 seems to be extended to
lst(Random(255)) ) = lst(Random(255)) +1
which leads to really strange behaviour.

Code:
Dim lst(255)
For t=0 To 100000
  lst(Random(255))+1
Next 

For t=0 To 255
  Debug lst(t)
  sum + lst(t)
Next 

Debug "sum (should be 100001):"+Str(sum)


For a "For"-loop there is a similar behaviour (although not so problematically), because the "To-conditions" is evaluated more than once.


Top
 Profile  
Reply with quote  
 Post subject: Re: PB 5.11b1 bug with short value assignment
PostPosted: Sun Mar 10, 2013 11:09 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Dec 03, 2011 5:54 pm
Posts: 736
Location: Oldenburg (Germany)
Strange - this way it works:
Code:
Dim lst(255)
For t = 0 To 100000
  i = Random(255)
  lst(i) + 1
Next

For t = 0 To 255
  Debug lst(t)
  sum + lst(t)
Next

Debug "sum (should be 100001):" + Str(sum)

_________________
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2


Top
 Profile  
Reply with quote  
 Post subject: Re: PB 5.11b1 bug with short value assignment
PostPosted: Sun Mar 10, 2013 11:13 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Apr 27, 2007 12:38 pm
Posts: 589
Location: Finland
IIRC this has been discussed before, and the verdict was no bug.


Top
 Profile  
Reply with quote  
 Post subject: Re: PB 5.11b1 bug with short value assignment
PostPosted: Sun Mar 10, 2013 11:15 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3704
Location: Berlin, Germany
Yes, this can cause problems. It has been discussed before and is by design, no bug.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: PB 5.11b1 bug with short value assignment
PostPosted: Sun Mar 10, 2013 11:33 am 
Offline
User
User

Joined: Sun Sep 16, 2012 12:13 pm
Posts: 65
really sad that the purebasic team is not willing to fix this obvious bug... :?
old bugreport


Top
 Profile  
Reply with quote  
 Post subject: Re: PB 5.11b1 bug with short value assignment
PostPosted: Sun Mar 10, 2013 11:42 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3704
Location: Berlin, Germany
codeprof wrote:
really sad that the purebasic team is not willing to fix this obvious bug... :?

viewtopic.php?p=379010#p379010
viewtopic.php?p=407434#p407434

If you want this to get changed, you can make a feature request.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: PB 5.11b1 bug with short value assignment
PostPosted: Sun Mar 10, 2013 11:53 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Aug 03, 2003 12:06 am
Posts: 264
Location: Austria
Where do you see a bug?

Code:
lst(Random(255))
standing alone is senseless - you have to assign something

Code:
lst(Random(255))= variable
or
Code:
lst(Random(255))=1+variable


In PB instead of using
Code:
lst(Random(255))+=1
as in other (script)languages you can use
Code:
lst(Random(255))+1

A great feature!
RGR

.

_________________
--------------
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg


Top
 Profile  
Reply with quote  
 Post subject: Re: PB 5.11b1 bug with short value assignment
PostPosted: Sun Mar 10, 2013 12:16 pm 
Offline
User
User

Joined: Sun Sep 16, 2012 12:13 pm
Posts: 65
The problem is that e.g.
Code:
lst(Random(255))+1

the compiler replaces Random() with two different random numbers.
So it will be replaced e.g. by:
Code:
lst(55)= lst(201)+1

However this not really obviously.
It shoud be:
Code:
lst(55)= lst(55)+1


Top
 Profile  
Reply with quote  
 Post subject: Re: PB 5.11b1 bug with short value assignment
PostPosted: Sun Mar 10, 2013 12:27 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Aug 03, 2003 12:06 am
Posts: 264
Location: Austria
codeprof wrote:
The problem is that e.g.
Code:
lst(Random(255))+1

the compiler replaces Random() with two different random numbers.
So it will be replaced e.g. by:
Code:
lst(55)= lst(201)+1

However this not really obviously.
It shoud be:
Code:
lst(55)= lst(55)+1


No it should not!
If you want to add 1 to a certain array you must program it right
Code:
x=random(255)
lst(x)+1

you cannot expect that
Code:
lst(Random(255))=lst(Random(255))+1
uses the same random number

As I said before var+1 in PB replaces var+=1 in other languages which is precompiled to var=var+1 or Array(x)+1 gets Array(x)=Array(x)+1
or lst(Random(255))+1 gets lst(Random(255))=lst(Random(255))+1

RGR

Edited:
A feature I don't want to miss in the future!

.

_________________
--------------
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg


Top
 Profile  
Reply with quote  
 Post subject: Re: PB 5.11b1 bug with short value assignment
PostPosted: Sun Mar 10, 2013 12:48 pm 
Offline
User
User

Joined: Sun Sep 16, 2012 12:13 pm
Posts: 65
But PB is the only programming language I know which is doing it this way.
Others like C,C# won't call Random() twice.


Top
 Profile  
Reply with quote  
 Post subject: Re: PB 5.11b1 bug with short value assignment
PostPosted: Sun Mar 10, 2013 12:56 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Aug 03, 2003 12:06 am
Posts: 264
Location: Austria
And we need Mister CodeProfessor to show us the right way?
I work since 2003 with PB - you joined 2012
I am glad with it 10 years now - you obviously not (see the other feature request thread you just started)

No comment anymore
RGR

_________________
--------------
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg


Top
 Profile  
Reply with quote  
 Post subject: Re: PB 5.11b1 bug with short value assignment
PostPosted: Mon Mar 11, 2013 12:51 am 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3693
Location: Italy
codeprof wrote:
But PB is the only programming language I know which is doing it this way.
Others like C,C# won't call Random() twice.


I agree with you it is strange and I never expected for it to work that way from previous experiences with other languages, the documentation IMO is misleading and routinely someone come up with this. Nevertheless the official explanation was it was made so by design, so you just have too keep in mind the way it actually works. At this point I don't think is going to change.

If you want to have more fun reading about this just follow the link I just posted here -> viewtopic.php?p=407480#p407480

_________________
[ My little PureBasic review ]


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

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 11 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