Some more math funcs

Share your advanced PureBasic knowledge/code with the community.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Some more math funcs

Post by Psychophanta »

Code: Select all

Procedure.f Sinh(x.f)
  ProcedureReturn (Pow(2,x/Log(2))-Pow(2,-x/Log(2)))/2
EndProcedure
Procedure.f Cosh(x.f)
  ProcedureReturn (Pow(2,x/Log(2))+Pow(2,-x/Log(2)))/2
EndProcedure
Procedure.f Tanh(x.f)
  ProcedureReturn Sinh(x.f)/Cosh(x.f)
EndProcedure
Procedure.f CoTanh(x.f)
  ProcedureReturn Cosh(x.f)/Sinh(x.f)
EndProcedure
Procedure.f Sech(x.f)
  ProcedureReturn 1/Cosh(x.f)
EndProcedure
Procedure.f CoSech(x.f)
  ProcedureReturn 1/Sinh(x.f)
EndProcedure
Procedure.f ePow(x.f)
  ProcedureReturn Pow(2,x/Log(2))
EndProcedure
Procedure.f ATan2(y.f,x.f)
  !fld dword[esp]
  !fld dword[esp+4]
  !fpatan
EndProcedure
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

That ATan2() definetely doesn't look right. Shouldn't it compute x/y (with y as the second argument) and then return an angle in the correct quadrant? Where's the four if checks for the quadrants?
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Trond wrote:Shouldn't it compute x/y?
Where's the four if checks for the quadrants?
Nope.
It computes Atan(y/x) and the only check is that when x=0 it returns Pi/2 or -Pi/2 depending on the sign of y . It doesn't check for quadrant.
However, the quadrant result is almost obvious:
If 0<result<PI/2 => 1st quadrant
If PI/2<result<PI => 2nd quadrant
If -PI<result<-PI/2 => 3rd quadrant
If -PI/2<result<0 => 4th quadrant
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post by eddy »

necro poster Image

PB4.40b2

I didn't manage to test your atan2 ASM function.
Here is my version of ATAN2 :

Code: Select all

Procedure.f ATan2(x.f, y.f)
   res.f=ATan(y/x)
   If x<0
      res+3.141592653589793238
   ElseIf y<0
      res+6.283185307179586477
   EndIf
   ProcedureReturn res
EndProcedure


Debug ATan2(0, 100)*180/#PI
Debug ATan2(100, 100)*180/#PI
Debug ATan2(100, 0)*180/#PI


Debug ATan2(-100, 100)*180/#PI
Debug ATan2(-100, 0)*180/#PI

Debug ATan2(-100, -100)*180/#PI
Debug ATan2(0, -100)*180/#PI

Debug ATan2(100, -100)*180/#PI
Last edited by eddy on Wed Sep 16, 2009 1:42 am, edited 1 time in total.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Post by Guimauve »

This is my library of custom Maths function.

Regards

Guimauve

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<
; <<<<< Cosecante <<<<<

Macro Cosec(theta)
  
   (1/Sin(theta))
  
EndMacro 

; <<<<<<<<<<<<<<<<<<<
; <<<<< Secante <<<<<

Macro Sec(theta)
  
   (1/Cos(theta))
  
EndMacro 

; <<<<<<<<<<<<<<<<<<<<<<
; <<<<< Cotangente <<<<<

Macro Cotan(theta)
  
   (Cos(theta)/Sin(theta))
  
EndMacro 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Sinus Hyperbolique <<<<<

Macro SinH(Angle)
  
   ((Pow(2.71828182846, Angle) - Pow(2.71828182846, -Angle)) / 2)
  
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Cosinus Hyperbolique <<<<<

Macro CosH(Angle)
  
   ((Pow(2.71828182846, Angle) + Pow(2.71828182846, -Angle)) / 2)
  
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Tangente Hyperbolique <<<<<

Macro TanH(Angle)
  
   (((Pow(2.71828182846, Angle) - Pow(2.71828182846, -Angle))) / ((Pow(2.71828182846, Angle) + Pow(2.71828182846, -Angle))))
  
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< CoTangente Hyperbolique <<<<<

Macro CotanH(Angle)

  (((Pow(2.71828182846, Angle) + Pow(2.71828182846, -Angle))) / ((Pow(2.71828182846, Angle) - Pow(2.71828182846, -Angle))))
  
EndMacro 
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re:

Post by Psychophanta »

eddy wrote:necro poster Image

PB4.41b2

I didn't manage to test your atan2 ASM function.
Here is my version of ATAN2 :

Code: Select all

Procedure.f ATan2(x.f, y.f)
   res.f=ATan(y/x)
   If x<0
      res+3.141592653589793238
   ElseIf y<0
      res+6.283185307179586477
   EndIf
   ProcedureReturn res
EndProcedure


Debug ATan2(0, 100)*180/#PI
Debug ATan2(100, 100)*180/#PI
Debug ATan2(100, 0)*180/#PI


Debug ATan2(-100, 100)*180/#PI
Debug ATan2(-100, 0)*180/#PI

Debug ATan2(-100, -100)*180/#PI
Debug ATan2(0, -100)*180/#PI

Debug ATan2(100, -100)*180/#PI
:?:
4.41b2 version does not exist, does it?

Well, PB versions has changed in time pass that is why it does not work. This one works now:

Code: Select all

Procedure.f ATan2(y.f,x.f)
  !fld dword[p.v_y]
  !fld dword[p.v_x]
  !fpatan
  ProcedureReturn
EndProcedure
But for me is a surprise the now there have to write ProcedureReturn to return a float value using floating point intel ASM stack. :!: :?:

EDIT: AArgh! stupid me! i forget since time ago ProcedureReturn is needed to return the float value content when using floating point i386 ASM in a Procedure!
I already have lots of functions in FPU i386 ASM in PB, but last days i have no time to take with PB and i had forgot it...

I wish i have more time for PB programming, but i have not enough atm. :roll:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply