View Full Version : Mandelbrot 3D set
testing the forum 3d mandelbrot set example in thinbasic with some variations, it is excelent. but trying the same (almost) in PB 9 result in a confusing status
Mandel3D thinbasic example: display 3d mandel set and it will also save the set as points so we can view the points in meshlab as xyz model.
Read_3D_Points TB example: will read the dataXYZ.xyz file produced by the Mandel3D and display it quickly.
Read_3D_Points.Bas : will read the dataXYZ.xyz file produced by the Thinbasic first example
note if we used leng = calcleng2(x,y,z) it will display the Set okay, but use leng = calcleng(x,y,z) and it will display it as spherical
can't believe since the 2 subs is the same, arn't the vars inside the subs protected and local ? why it works in thinbasic excellently
Mandel3D.bas: in PB in line 137 if we use leng = calcleng(x,y,z) it will display spherical shape. but if we use instead leng = calcleng2(x,y,z) which is the same but changing the variables names, it will display the set as if it is compressed
mike lobanovsky
28-06-2018, 02:25
Hi primo,
Disclaimer: I have not run your code, I have only looked through it. Still my comments can lead you in the proper direction for you to be able to understand and debug your code yourself.
In fact, PowerBASIC and thinBasic pass their function arguments differently on default: by reference and by copy, respectively. This means that before passing the arguments into the function, TB would make copies of them thus isolating whatever changes the function might make to their values, from the main code that called the function. OTOH PB would pass the arguments by reference so that every change made to the argument values is immediately propagated back to the caller code.
Your calcleng function modifies the original x, y, z arguments heavily but the mods can't affect the TB caller code because the arguments aren't in fact stored in the exact memory locations where they reside in the caller code. However the same function used in PB will propagate the mods back to the caller code and will thus spoil the calculation results for the subsequent iterations. At the same time, calcleng2 seen in the PB code uses local temp vars as buffers that isolate the mods from the function's original arguments passed by the caller code. That's why that function would perform identically to the TB calcleng function, while the PB calcleng function would fail.
Make it a habit to use explicit ByRef and ByVal parameter qualifiers to declare your function parameters in the both languages if you want to avoid similar pitfalls in the future.
Thank you mike very much for the help, i forgot BYVAL/BYREF completely as if they does not exist due to the lack of usage, yes adding BYVAL to PB solve the problem and display Mandel3D correctly:
calcleng( BYVAL x AS SINGLE, BYVAL y AS SINGLE, BYVAL z AS SINGLE) AS SINGLE
it seems we don't need to declare calcleng previously. as it works with and without "DECLARE FUNCTION calcleng" .
i never used BYVAL/BYREF in thinbasic so i have tested thinbasic now
and only BYREF change the x variable:
Uses "Console"
Local x, y As Long
x = 2
y = test(x)
PrintL "x= " + x
PrintL "y= " + y
PrintL:PrintL "press any key to exit"
WaitKey
'Function test(ByRef x As Long) As Long
'Function test(ByVal x As Long) As Long
Function test(x As Long) As Long
Local y
x = x + 5
y = x
Function = y
End Function
thanks and best regards
mike lobanovsky
28-06-2018, 09:56
You're welcome, primo.
Terminology:
DECLARE FUNCTION Foo LIB "Bar.dll" (ByVal param AS LONG) AS LONG ' this is called "forward declaration"
FUNCTION Foo(ByVal param AS LONG) AS LONG ' this is called "declaration"
param += 1 ' \
FUNCTION = param ' > this is called "implementation"
END FUNCTION ' /
Petr Schreiber
05-07-2018, 10:28
Just for the record,
ThinBASIC functions parameters are ByVal by default (as it is less confusing), while PowerBASIC has parameters ByRef (as it is faster).
Petr