PB 5.11b1 bug with short value assignment

Post bugreports for the Windows version here
codeprof
User
User
Posts: 65
Joined: Sun Sep 16, 2012 12:13 pm

PB 5.11b1 bug with short value assignment

Post by codeprof »

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: Select all

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.
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: PB 5.11b1 bug with short value assignment

Post by uwekel »

Strange - this way it works:

Code: Select all

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
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Re: PB 5.11b1 bug with short value assignment

Post by eesau »

IIRC this has been discussed before, and the verdict was no bug.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: PB 5.11b1 bug with short value assignment

Post by Little John »

Yes, this can cause problems. It has been discussed before and is by design, no bug.
codeprof
User
User
Posts: 65
Joined: Sun Sep 16, 2012 12:13 pm

Re: PB 5.11b1 bug with short value assignment

Post by codeprof »

really sad that the purebasic team is not willing to fix this obvious bug... :?
old bugreport
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: PB 5.11b1 bug with short value assignment

Post by Little John »

codeprof wrote:really sad that the purebasic team is not willing to fix this obvious bug... :?
http://www.purebasic.fr/english/viewtop ... 10#p379010
http://www.purebasic.fr/english/viewtop ... 34#p407434

If you want this to get changed, you can make a feature request.
Thade
Enthusiast
Enthusiast
Posts: 266
Joined: Sun Aug 03, 2003 12:06 am
Location: Austria

Re: PB 5.11b1 bug with short value assignment

Post by Thade »

Where do you see a bug?

Code: Select all

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

Code: Select all

lst(Random(255))= variable
or

Code: Select all

lst(Random(255))=1+variable
In PB instead of using

Code: Select all

lst(Random(255))+=1
as in other (script)languages you can use

Code: Select all

lst(Random(255))+1
A great feature!
RGR

.
--------------
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg
codeprof
User
User
Posts: 65
Joined: Sun Sep 16, 2012 12:13 pm

Re: PB 5.11b1 bug with short value assignment

Post by codeprof »

The problem is that e.g.

Code: Select all

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

Code: Select all

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

Code: Select all

lst(55)= lst(55)+1
Thade
Enthusiast
Enthusiast
Posts: 266
Joined: Sun Aug 03, 2003 12:06 am
Location: Austria

Re: PB 5.11b1 bug with short value assignment

Post by Thade »

codeprof wrote:The problem is that e.g.

Code: Select all

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

Code: Select all

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

Code: Select all

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: Select all

x=random(255)
lst(x)+1
you cannot expect that

Code: Select all

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
codeprof
User
User
Posts: 65
Joined: Sun Sep 16, 2012 12:13 pm

Re: PB 5.11b1 bug with short value assignment

Post by codeprof »

But PB is the only programming language I know which is doing it this way.
Others like C,C# won't call Random() twice.
Thade
Enthusiast
Enthusiast
Posts: 266
Joined: Sun Aug 03, 2003 12:06 am
Location: Austria

Re: PB 5.11b1 bug with short value assignment

Post by Thade »

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
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: PB 5.11b1 bug with short value assignment

Post by luis »

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 -> http://www.purebasic.fr/english/viewtop ... 80#p407480
"Have you tried turning it off and on again ?"
A little PureBasic review
Post Reply