Oxygen Basic
Programming => Bugs & Feature Requests => Topic started by: Brian Alvarez on May 09, 2019, 10:33:19 PM
-
This code:
floor(l1 + ((¤rand() / 32767) * (l2 - l1)))
Fails... while this:
double rv = (¤rand() / 32767)
print floor(l1 + (rv * (l2 - l1)))
Gives a good result.
Also this:
print (¤rand() / 32767) + " THE RESULT"
Omits the "THE RESULT" string. Seems like the one liner aborts after the function calls.
Would you help me get the latest version of oxygen up and running? I dont know what i might be doing wrong....
-
I just upgraded to the newest version of Oxygen. The issue i was having before upgrading, still occurs in the newest version:
declare function rand lib "msvcrt.dll" alias "rand" () as int
declare function srand lib "msvcrt.dll" alias "srand" (int seed)
FUNCTION rnd(byval int l1, l2) AS DOUBLE
if l1 > l2 then
int l3 = l1
l1 = l2
l2 = l3
end if
double rv = (rand() / 32767)
return floor(l1 + (rv * (l2 - l1)))
END FUNCTION
srand(21123) ' seed the randomizer.
int v
int i
for i = 1 to 10
v = 2000 + rnd(1, 20) 'this fails. always returns a number smaller than 21
'v = rnd(1, 20) + 2000 'this works. this returns a number from 2001 to 2020.
print " r: " v chr(13, 10)
next i
-
If i change the data type of the rnd() function from DOUBLE to LONG it works.
SINGLE also fails....
I believe this is a fundamental bug that will affect many other situations...
-
Simpler test:
FUNCTION rnd(byval int l1, l2) AS QUAD
return 1000
END FUNCTION
int v = 0
v = 2000 + rnd(1, 20) 'this fails. produces: 2446896
'v = rnd(1, 20) + 2000 'this works. produces 3000
print v
-
Yes, I see the problem.
It concerns mixed float functions and integers. If you make the dest variable v a float. It will work as expected. I will investigate further.
The print problem is a separate issue:
'print (¤rand() / 32767) + " THE RESULT"
print( (¤rand() / 32767) + " THE RESULT" )
-
Thanks charles! :)
Regarding the print issue, i can also see the logic. It is taking the bracketed part as the parameters of the print function.
Maybe a syntax error should be raised for the + after the brackets?
Added: Nah, nvm. Less error checking is faster. I will make sure to trap syntax errors here.
-
One solution is to insert a null string literal after print:
I'll make o2 do this automatically, whenever the first print term is other than a quote mark.
-
Clever...