Page 1 of 2

Macro, a bug ?

Posted: Mon Jul 01, 2024 4:23 pm
by threedslider
Someone has reported me some problem in my code for macro

I find it maybe some bug for macro, try it :

Code: Select all

Macro m_random_double() : Random(#MAXDWORD) / #MAXDWORD : EndMacro

Procedure.d p_random_double()
  ProcedureReturn (Random(#MAXDWORD) / #MAXDWORD)
EndProcedure

Debug m_random_double()

Debug p_random_double()
output for debug

Code: Select all

0
0.9093949617607041124855982

Re: Macro, a bug ?

Posted: Mon Jul 01, 2024 4:32 pm
by Fred
The first call is integer only, while the second expect double as result. All seems OK here.

Re: Macro, a bug ?

Posted: Mon Jul 01, 2024 4:43 pm
by threedslider
Oh sorry about that...

But... Can you explain me why it works in double this macro :

Code: Select all

Macro random_double() : Random(#MAXDWORD) / #MAXDWORD : EndMacro
Macro random_min_max(min, max) : min + (max-min) * random_double() : EndMacro

Debug random_min_max(-1.0, 1.0)
Output

Code: Select all

0.2273651229281363761458579
It sounds strange to me ... Is it supposed to be integer right ? :shock:

Re: Macro, a bug ?

Posted: Mon Jul 01, 2024 4:46 pm
by infratec

Code: Select all

Macro m_random_double() : (Random($7FFFFFF) / 1.0 / $7FFFFFF) : EndMacro

Re: Macro, a bug ?

Posted: Mon Jul 01, 2024 4:47 pm
by DarkDragon
threedslider wrote: Mon Jul 01, 2024 4:43 pm Oh sorry about that...

But... Can you explain me why it works in double this macro :

Code: Select all

Macro random_double() : Random(#MAXDWORD) / #MAXDWORD : EndMacro
Macro random_min_max(min, max) : min + (max-min) * random_double() : EndMacro

Debug random_min_max(-1.0, 1.0)
Output

Code: Select all

0.2273651229281363761458579
It sounds strange to me ... Is it supposed to be integer right ? :shock:
Because -1.0 and 1.0 are double. If you have a pure integer expression it will be calculated as integer. As soon as a double is involved it's a double expression. You have to get used to it. It's a bit different than some other languages where you need explicit casting.

Re: Macro, a bug ?

Posted: Mon Jul 01, 2024 4:51 pm
by mk-soft
The first value type or assignment type decides how the following values are handled

Code: Select all

#MAXDWORD = 1000

Macro m_random_double() : Random(#MAXDWORD) / #MAXDWORD : EndMacro

Debug 1.0 * m_random_double() ; <- Force cast to float or double

r1.d = m_random_double()
Debug "" + r1 + " Random Result"; <- Force r1 to string
;Debug r1 + " Random Result"; <- Inalid 
---

Code: Select all

; 50 %

a = 20
b = 50

Debug a / 100 * b ; Integer calculation error by the programmer.

Debug 1.0 * a / 100 * b ; Force to float

c = 1.0 * a / 100 * b ; Force to float and back integer
Debug c

c = a * b / 100 ; Correct integer calculation
Debug c

Re: Macro, a bug ?

Posted: Mon Jul 01, 2024 5:07 pm
by threedslider
Ok thanks all guys, never thought this casting for macro, it works here.

@mk-soft : #MAXDWORD has already declared from PB, impossible to assign a new value... sorry :?
But... without to assign to a new value it works your code ! :)

Re: Macro, a bug ?

Posted: Mon Jul 01, 2024 5:11 pm
by threedslider
@mk-soft : Strange this 1.0 * m_random_double() work as double
But r1.d = m_random_double() ... not same here for precision, it looks like more shorter than first

Re: Macro, a bug ?

Posted: Mon Jul 01, 2024 5:12 pm
by mk-soft
threedslider wrote: Mon Jul 01, 2024 5:07 pm @mk-soft : #MAXDWORD has already declared from PB, impossible to assign a new value... sorry :?
But... without to assign to a new value it works your code ! :)
#MAXDWORD is probably a Windows API constant. It does not exist under Linux or macOS

Re: Macro, a bug ?

Posted: Mon Jul 01, 2024 5:16 pm
by pjay
Try moving to a float-based constant - by adjusting it to a multiplier will make it (slightly) faster also:

Code: Select all

#MAXDWORDMultiplier = 1.0 / #MAXDWORD 
Macro m_random_double() : Random(#MAXDWORD) * #MAXDWORDMultiplier : EndMacro

Debug m_random_double()

Re: Macro, a bug ?

Posted: Mon Jul 01, 2024 5:21 pm
by mk-soft
threedslider wrote: Mon Jul 01, 2024 5:11 pm @mk-soft : Strange this 1.0 * m_random_double() work as double
But r1.d = m_random_double() ... not same here for precision, it looks like more shorter than first
Default StrD(...) NbDecimals limit

Code: Select all

Macro m_random_double() : Random(#MAXDWORD) / #MAXDWORD : EndMacro

r1.d = m_random_double()
Debug "" + r1 + " Random Result"; <- Force r1 to string
Debug StrD(r1)
Debug StrD(r1, 15)
Debug r1

Re: Macro, a bug ?

Posted: Mon Jul 01, 2024 5:35 pm
by threedslider
@pjay Oh that is better one, thank you ! :oops:

@mk-soft : Ok understood ! I learn everyday this great PB :)

Re: Macro, a bug ?

Posted: Mon Jul 01, 2024 6:27 pm
by infratec

Re: Macro, a bug ?

Posted: Mon Jul 01, 2024 6:56 pm
by Quin
infratec wrote: Mon Jul 01, 2024 4:46 pm

Code: Select all

Macro m_random_double() : (Random($7FFFFFF) / 1.0 / $7FFFFFF) : EndMacro
You're a wizard!

Re: Macro, a bug ?

Posted: Mon Jul 01, 2024 7:13 pm
by threedslider
infratec wrote: Mon Jul 01, 2024 6:27 pm The solution was posted very early :wink:

https://www.purebasic.fr/english/viewto ... 23#p623423
Yeah I know both you and pjay are great ! I prefer to pjay (it is my taste ;) )

But your code is working as strangely ! Twin division ! :shock: