[PB5.21] bug with Random() in Macro

Just starting out? Need help? Post your questions and find answers here.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

[PB5.21] bug with Random() in Macro

Post by Psychophanta »

With PB 5.21 it returns an error of negative value for Random() PB function, but the value is never negative. :shock:

With PB 5.00 works well. :wink:

Code: Select all

Define .d
Macro RND(v1,v2,ndecimales=3)
  (Random(1E#ndecimales#)*(v2#-v1#)/1E#ndecimales#+v1#)
EndMacro

v=RND(1E-18,1.0,12)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User_Russian
Addict
Addict
Posts: 1517
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: [PB5.21] bug with Random() in Macro

Post by User_Russian »

Outside macro is similar.

Code: Select all

Debug Random(1E12)
Macro code.

Code: Select all

v=(Random(1E12)*(1.0-1E-18)/1E12+1E-18)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: [PB5.21] & [PB5.22b1] bug with Random() in Macro

Post by Psychophanta »

From manual:
Syntax

Result = Random(Maximum)
Description

Returns a random number between 0 and the given maximum value.
Parameters

Maximum The maximum value. It may not exceed the maximum positive integer value.
What happens if run for example Random($100000000) in a 64 bit PB?

The bug is in the fact that in a 32 bit environment there is always returned '0' for that command, and the compiler neither the Purifier do not complain.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: [PB5.21] bug with Random() in Macro

Post by luis »

Psychophanta wrote:With PB 5.21 it returns an error of negative value for Random() PB function, but the value is never negative. :shock:
1E12 is a double larger than an integer on x86, random() is expecting an integer.
The overflow is causing the signed integer to become negative.

If you pass an integer up to max positive integer value as specified in the doc nothing bad happens.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: [PB5.21] bug with Random() in Macro

Post by Psychophanta »

luis wrote: If you pass an integer up to max positive integer value as specified in the doc nothing bad happens.
And if you pass a negative quad value nothing bad happens. 8)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: [PB5.21] bug with Random() in Macro

Post by luis »

Psychophanta wrote: And if you pass a negative quad value nothing bad happens. 8)
Still the same reason.

The doc says you have to pass a POSITIVE integer.

In your original example you are passing a literal double (1e12) which becomes a negative integer.
So you get the debugger error.

Code: Select all

d.d = 1e12
i.i = d

Debug i ; -2147483648 -> DOH ! IT'S NEGATIVE BECAUSE I'M PASSING THE WRONG STUFF !
When you pass a literal quad, since random() expect an integer only half of its bits are passed to the random() function.
Those half bits can represent a positive integer number and so you don't get the debugger error.

Code: Select all

q.q = -9223372036854775000

i.i = q

Debug "q = " + RSet(Bin(q),64,"0") 
Debug "i = " + RSet(Bin(i),64,"0")

Debug i ; 808 -> DOH ! IT'S POSITIVE, ONLY WRONG BECAUSE I'M PASSING THE WRONG STUFF !

Code: Select all

Debug Random(-9223372036854775000) ; just seems to work (sorta) and to not complain, in reality this is Debug Random(808) so it has no reason to complain.
You should not pass a quad (not too wide at least), you should not pass a double (not too big at least).

Can't you just use the PB command as it is documented and pass a positive integer ? I don't see the problem.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: [PB5.21] bug with Random() in Macro

Post by Psychophanta »

Sorry man, but in my first example i pass 1E-18, which is NOT a negative number, in opposition to you say and the compiler says.
1E12 IS NOT a negative value either.

So, PLEASE, be rigorous, and don't try to convince everybody about those lies.

Thanks! 8)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: [PB5.21] bug with Random() in Macro

Post by luis »

Psychophanta wrote: So, PLEASE, be rigorous, and don't try to convince everybody about those lies.
You are calling what you don't understand lies.
Psychophanta wrote:Sorry man, but in my first example i pass 1E-18, which is NOT a negative number, in opposition to you say and the compiler says.
Oh, you sad excuse for a wanna-be programmer... try to follow this time.

The 1E-18 you are TRYING to pass it's not going anywhere. You should try to understand how macros works and how the one you wrote is actually expanded.

Your crap

Code: Select all

v=RND(1E-18,1.0,12)
is expanded this way (as User_Russian already wrote without being able to reach you)

Code: Select all

v=(Random(1E12)*(1.0-1E-18)/1E12+1E-18)
You are passing 1E-18, random() is getting 1E12 because you don't know what the hell you are doing (probably just a mistake really, but your firm belief that what you wrote is perfect prevents you from looking at it twice even after the error is shown to you).

Psychophanta wrote: 1E12 IS NOT a negative value either.
I've explained it to you with a simple example, if you don't understand it it's fine. It happens.

What's not fine it's your attitude. You don't even try. You don't welcome people trying to help you, you piss them off. This is not the first time.

Your only guide is ignorance coupled with an arrogance your persona unfortunately can't sustain or afford.

Bad way to live Psycho, so I'll leave you to it.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: [PB5.21] bug with Random() in Macro

Post by Psychophanta »

Psychophanta wrote:...in my first example i pass 1E-18...
Wrong (i.e. lie)!
I am passing 1E12 to the Random() function.
However, as said: 1E12 is not a negative value, neither 1E-18.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: [PB5.21] bug with Random() in Macro

Post by Psychophanta »

Bug still present in 5.43LTS and 5.50 versions:
Big numbers inconsistence behaviour
Comments in the code:

Code: Select all

Define .d

Macro RND(v1,v2,ndecimales=3)
  (Random(1E#ndecimales#)*(v2#-v1#)/1E#ndecimales#+v1#)
EndMacro

v=RND(1E-18,1.0,11)
Debug v
;NOTICE: (Pow(2,31)-1 = 2147483647.0), which is higher than (1E11 = 100000000000.0). But NO error reported by compiler.

;but:

; v=RND(1E-18,1.0,12)
; Debug v
;NOTICE: (Pow(2,31)-1 = 2147483647.0), which is higher than (1E12 = 1000000000000.0). But ERROR reported by compiler.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply