Oxygen Basic
Information => Development => Topic started by: erosolmi on October 20, 2013, 11:16:10 AM
-
http://www.thinbasic.com/community/showthread.php?t=12258&p=90147#post90147
Hi Charles,
Robbek user at thinBasic forum is using embedded Oxygen for some interesting math.
It seems pow(-2, 2) gives -4
Can it be?
This is the thinBasic script ebmedding Oxygen showing the problem.
Uses "oxygen"
Dim pp As Single
O2_ASM "
Dim pp At #pp As Single
pp = Pow(-2,2)
"
O2_EXEC
MsgBox 0, pp
-
Hi Eros,
I did a simple test and it works fine here.
See attachments.
.
-
I think I have an old Oxygen version in thinBasic.
thinBasic has a special version of Oxygen so I need to wait Charles reply.
Thanks Ken
-
Thanks Eros,
I've been sitting on the new thinBasic_Oxygen module for quite a while. I think it is ready to include with thinBasic. It is a very small module that embeds the generic Oxygen.dll.
I've included a few key examples.
http://www.thinbasic.com/community/showthread.php?t=12175
-
Hi Charles,
sorry I missed your update.
I'm trying this version and it shows a message box titled "O2H" with message "lib\oxygen.dll"
I think it means that oxygen.dll was not found.
I suppose because you hard coded "\lib\" directory instead it should be loaded from the same directory where "thinBasic_Oxygen.dll" wrapper DLL is loaded from.
Do you think it can be changed?
Correct path is passed by thinCore.dll to thinBasic_Oxygen.dll in LoadLocalSymbols function as sPath param
Thanks a lot
Eros
-
I think I have an old Oxygen version in thinBasic.
thinBasic has a special version of Oxygen so I need to wait Charles reply.
Thanks Ken
That makes sense should of thought of that. I see Charles replied so will leave it in the 2 masters hands.
-
It should work when Oxygen.dll is placed in lib, with the new thinBasic_Oxygen.dll.
This implies that users who use both OxygenBasic and thinBasic as separate systems will have the oxygen.dll in 2 places.
I was able to produce the error you saw by removing oxygen.dll from lib
-
For the way thinBasic has been built, having a path hard coded is not good.
DLL must not have any paths hard coded, otherwise it would be a nightmare.
For example:
- during development the modules is into a directory
- when thinBasic is released to users, modules are into another directory
- when a script is embedded into a bundled exe, modules are into another directory dynamically decided at run-time
That's the reason why thinBasic pass to each thinBasic modules the path from which they have been loaded: in order to be aware of it and do the correct assumptions.
Do you think to have a chance to change current behave?
Otherwise the only way I have to make it working is that I will make another wrapper instead of your thinBasic_Oxygen.dll and dynamically load Oxygen.dll at run-time from the same directory of the wrapper.
Ciao
Eros
-
Done :)
http://www.thinbasic.com/community/showthread.php?t=12258&page=2&p=90154#post90154
-
Hi Charles,
Robbek user at thinBasic forum is using embedded Oxygen for some interesting math.
It seems pow(-2, 2) gives -4
Can it be?
PRINT -2^2,"\n"
jrs@laptop:~/sb22$ scriba testpow.sb
4
jrs@laptop:~/sb22$
-
Charles,
thanks a lot!!
It works great now.
Ciao
Eros
-
Eros & Charles,
I'm really happy to see your relationship (O2 & TB) being renewed and enhanced. I think a better job needs to be done to help users understand and use O2 with thinBasic. I see a real opportunity for both TB and O2 as PB struggles with relevance. ThinBasic is PowerBASIC done right and O2 is the hand crafted ASM it lacks. You guys make a great team that can't be beat. Please don't lose sight of that again.
John
-
Thanks Charles, Eros .. it gives the correct positive value now.
Amazing program Eros, ! -- finally I ran it , very impressing those real-time inter-actions.
This all made me curious about that pow(x,y) function on several programming languages.
I noticed that when y is not an integer, both Thinbasic and the GFA compiler use the closest integer for y to get a real number result in case x < 0. (in case x is a negative number and y contains a fraction the result is a complex number )
In this case both Freebasic and the LISP give respec. QNAN and NAN -- something as "not an available number" iirc.
( .. mm , that NAN could be dangerous incase one gets 1.999999 that sould be 2 or so .... )
best Rob
PRINT FORMAT("%3.6f",-2^.2),"\n"
jrs@laptop:~/bas2nim/sb2n$ scriba testpow.sb
-1.148698
jrs@laptop:~/bas2nim/sb2n$
<?php
echo pow(-2, .2);
?>
jrs@laptop:~/bas2nim/b2n_py$ php testpow.php
NAN
jrs@laptop:~/bas2nim/b2n_py$
import math
print math.pow(-2, .2)
jrs@laptop:~/bas2nim/b2n_py$ python testpow.py
Traceback (most recent call last):
File "testpow.py", line 3, in <module>
print math.pow(-2, .2)
ValueError: math domain error
jrs@laptop:~/bas2nim/b2n_py$
#include <math.h>
main()
{
double a = -2.0;
double b = 0.2;
printf("%3.6f\n", pow(a,b));
}
jrs@laptop:~/bas2nim/sb2n$ ./testpow
-nan
jrs@laptop:~/bas2nim/sb2n$
-
Hi John,
Yes, there were too many examples in the earlier releases of thinBasic_Oxygen, many of which became outdated. I've cut them right down to a few key examples. I think the need for it will still be quite specialised though.
Eros is always very good to work with. Time is the only obstacle.
-
When dealing with fractional powers of negative numbers, you need complex numbers. A simple number cannot represent the result.
These can be solved using deMoivre's theorem, relating the power to an angle of rotation in the complex plane
This seems to work:
/*
http://www.suitcaseofdreams.net/De_Moivre_formula.htm
*/
type complex double x,y
function zpower(complex*z, double n)
'Using DeMoivre theorem
double radius,ang,scale
radius = hypot(z.x,z.y)
scale = radius^n
if z.x=0 then
angle=.5*pi '90 degrees
if z.y<0 then angle=1.5*pi
else
angle = atan(z.y,z.x)
end if
angle*=n
z.x = scale * cos(angle)
z.y = scale * sin(angle)
end function
complex n={-2.0 , 0.0}
zpower( n, 0.2)
print str(n.x,6) " + " str(n.y,6) "i"
A good test is -4^.5
expected result: {0 + 2i}
-
FOR x = -2 to -4 STEP -1
FOR y = .1 to .5 STEP .1
PRINT "x = ",x," y = ",FORMAT("%g",y)," - ",FORMAT("%3.6f",x^y),"\n"
NEXT y
NEXT x
jrs@laptop:~/sb/sb22/test$ scriba testpow.sb
x = -2 y = 0.1 - 0.000000
x = -2 y = 0.2 - -1.148698
x = -2 y = 0.3 - 0.000000
x = -2 y = 0.4 - 0.000000
x = -2 y = 0.5 - 0.000000
x = -3 y = 0.1 - 0.000000
x = -3 y = 0.2 - -1.245731
x = -3 y = 0.3 - 0.000000
x = -3 y = 0.4 - 0.000000
x = -3 y = 0.5 - 0.000000
x = -4 y = 0.1 - 0.000000
x = -4 y = 0.2 - -1.319508
x = -4 y = 0.3 - 0.000000
x = -4 y = 0.4 - 0.000000
x = -4 y = 0.5 - 0.000000
jrs@laptop:~/sb/sb22/test$
@Charles - Is SB spitting out garbage or is there some explanation for these results? (at least SB isn't spitting NAN's)
-
Is a Null-Complexe :D
-
No ...
less than zero complex... ;D
'po po po power....r..rr..
sub power(sys a,ex) as sys
sys res
if a < 0
res = -(a ^ ex)
else
res = a ^ ex
end if
return res
end sub
sys is
is = power(-2,2)
print str(is)