Page 1 of 1

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

Posted: Mon Mar 03, 2014 11:24 am
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)

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

Posted: Mon Mar 03, 2014 11:29 am
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)

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

Posted: Wed Mar 05, 2014 1:01 pm
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.

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

Posted: Tue Mar 18, 2014 9:06 pm
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.

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

Posted: Tue Mar 18, 2014 9:16 pm
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)

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

Posted: Tue Mar 18, 2014 11:04 pm
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.

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

Posted: Wed Mar 19, 2014 10:36 am
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)

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

Posted: Wed Mar 19, 2014 12:09 pm
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.

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

Posted: Wed Mar 19, 2014 10:34 pm
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.

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

Posted: Mon Aug 22, 2016 11:43 am
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.