Macro, a bug ?

Just starting out? Need help? Post your questions and find answers here.
threedslider
Enthusiast
Enthusiast
Posts: 434
Joined: Sat Feb 12, 2022 7:15 pm

Macro, a bug ?

Post 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
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Macro, a bug ?

Post by Fred »

The first call is integer only, while the second expect double as result. All seems OK here.
threedslider
Enthusiast
Enthusiast
Posts: 434
Joined: Sat Feb 12, 2022 7:15 pm

Re: Macro, a bug ?

Post 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:
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Macro, a bug ?

Post by infratec »

Code: Select all

Macro m_random_double() : (Random($7FFFFFF) / 1.0 / $7FFFFFF) : EndMacro
DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Macro, a bug ?

Post 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.
Last edited by DarkDragon on Mon Jul 01, 2024 4:56 pm, edited 1 time in total.
bye,
Daniel
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Macro, a bug ?

Post 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
Last edited by mk-soft on Mon Jul 01, 2024 5:07 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
threedslider
Enthusiast
Enthusiast
Posts: 434
Joined: Sat Feb 12, 2022 7:15 pm

Re: Macro, a bug ?

Post 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 ! :)
threedslider
Enthusiast
Enthusiast
Posts: 434
Joined: Sat Feb 12, 2022 7:15 pm

Re: Macro, a bug ?

Post 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
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Macro, a bug ?

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
pjay
Enthusiast
Enthusiast
Posts: 277
Joined: Thu Mar 30, 2006 11:14 am

Re: Macro, a bug ?

Post 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()
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Macro, a bug ?

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
threedslider
Enthusiast
Enthusiast
Posts: 434
Joined: Sat Feb 12, 2022 7:15 pm

Re: Macro, a bug ?

Post by threedslider »

@pjay Oh that is better one, thank you ! :oops:

@mk-soft : Ok understood ! I learn everyday this great PB :)
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Macro, a bug ?

Post by infratec »

Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Macro, a bug ?

Post 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!
threedslider
Enthusiast
Enthusiast
Posts: 434
Joined: Sat Feb 12, 2022 7:15 pm

Re: Macro, a bug ?

Post 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:
Post Reply