Author Topic: pow(-2,2)=-4  (Read 8683 times)

0 Members and 1 Guest are viewing this topic.

erosolmi

  • Guest
pow(-2,2)=-4
« 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.
Code: [Select]
  Uses "oxygen"

  Dim pp As Single
  O2_ASM "
          Dim pp At #pp As Single
          pp = Pow(-2,2)
          "
  O2_EXEC
  MsgBox 0, pp

kryton9

  • Guest
Re: pow(-2,2)=-4
« Reply #1 on: October 20, 2013, 12:00:17 PM »
Hi Eros,

I did a simple test and it works fine here.
See attachments.

.

erosolmi

  • Guest
Re: pow(-2,2)=-4
« Reply #2 on: October 20, 2013, 12:09:33 PM »
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

Charles Pegge

  • Guest
Re: pow(-2,2)=-4
« Reply #3 on: October 20, 2013, 12:30:02 PM »
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

erosolmi

  • Guest
Re: pow(-2,2)=-4
« Reply #4 on: October 20, 2013, 12:46:45 PM »
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

kryton9

  • Guest
Re: pow(-2,2)=-4
« Reply #5 on: October 20, 2013, 01:30:13 PM »
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.

Charles Pegge

  • Guest
Re: pow(-2,2)=-4
« Reply #6 on: October 20, 2013, 01:39:30 PM »
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
« Last Edit: October 20, 2013, 01:49:20 PM by Charles Pegge »

erosolmi

  • Guest
Re: pow(-2,2)=-4
« Reply #7 on: October 20, 2013, 08:56:56 PM »
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

Charles Pegge

  • Guest

JRS

  • Guest
Re: pow(-2,2)=-4
« Reply #9 on: October 21, 2013, 02:07:37 AM »
Quote from: Eros
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?

Code: [Select]
PRINT -2^2,"\n"

jrs@laptop:~/sb22$ scriba testpow.sb
4
jrs@laptop:~/sb22$

erosolmi

  • Guest
Re: pow(-2,2)=-4
« Reply #10 on: October 21, 2013, 10:56:18 AM »
Charles,

thanks a lot!!
It works great now.

Ciao
Eros

JRS

  • Guest
Re: pow(-2,2)=-4
« Reply #11 on: October 21, 2013, 11:08:09 AM »
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

JRS

  • Guest
Re: pow(-2,2)=-4
« Reply #12 on: October 21, 2013, 03:23:36 PM »
Quote
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

Code: [Select]
PRINT FORMAT("%3.6f",-2^.2),"\n"

jrs@laptop:~/bas2nim/sb2n$ scriba testpow.sb
-1.148698
jrs@laptop:~/bas2nim/sb2n$

Code: [Select]
<?php
echo pow(-2.2);
?>


jrs@laptop:~/bas2nim/b2n_py$ php testpow.php
NAN
jrs@laptop:~/bas2nim/b2n_py$


Code: [Select]
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$

Code: [Select]
#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$
« Last Edit: October 21, 2013, 04:36:05 PM by John »

Charles Pegge

  • Guest
Re: pow(-2,2)=-4
« Reply #13 on: October 22, 2013, 04:42:39 AM »
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.

Charles Pegge

  • Guest
Re: pow(-2,2)=-4
« Reply #14 on: October 22, 2013, 08:22:23 AM »
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:
Code: [Select]
 /*
  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}
« Last Edit: October 22, 2013, 08:32:21 AM by Charles Pegge »