[Solved][4.60 RC1] ATan2() parameters are switched

Just starting out? Need help? Post your questions and find answers here.
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

[Solved][4.60 RC1] ATan2() parameters are switched

Post by infratec »

Hi,

during implementation of haversine() algorithm I found out, that the parameters of the ATan2()
function are switched.
http://www.purebasic.fr/english/viewtop ... 26#p363826

A few comparrisons with 'C' code and an own implementation demonstrated this behaviour.

ATan2(0.0705, 0.9975) should return something arround 0.070559
But the current implementation results in 1.500237

My own creation

Code: Select all

Procedure.f atan2_bkk(y.f, x.f)
  
  Result.f = 0
  
  If x > 0
    Result = ATan(y/x)
  ElseIf x < 0
    Result = ATan(y/x) + #PI
  Else
    If y > 0
      Result = #PI / 2
    ElseIf y < 0
      Result = #PI / -2
    Else
      Result = 0
    EndIf
  EndIf
  
  ProcedureReturn Result
  
EndProcedure
works correct.

Than I tested ATan2() with switched parameters and it gaves the right result:

ATan2(0.9975, 0.0705) -> 0.070559

I tested it only in Windows, but I think this bug is also in the other versions.

Please can someone confirm this bug?

Bernd
Last edited by infratec on Fri Oct 14, 2011 1:20 pm, edited 2 times in total.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: [4.60 RC1] ATan2() parameters are switched

Post by Shield »

I also noticed that a couple of weeks ago.
In any other implementations I found, 'y' is specified first followed by 'x'.
But in PB it's the exact opposite where 'x' comes before 'y'.

So I wouldn't call this a bug, it's just a difference in parameter order.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: [4.60 RC1] ATan2() parameters are switched

Post by infratec »

Ohhhhhhh,

I have to say sorry to Fred :!:

In man page for gcc:
double atan2(double y, double x)


In the helpfile of PB:
Ergebnis.f = ATan2(x.f, y.f)
So it is no bug, but a very very ugly pitfall.

C, Java, Ruby, Python, PHP, JavaScript ...
All languages I found uses (y, x)

P.S.: Oh I found it 4 seconds to late :oops:
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: [4.60 RC1] ATan2() parameters are switched

Post by luis »

infratec wrote:
P.S.: Oh I found it 4 seconds to late :oops:
4 minutes :wink:
"Have you tried turning it off and on again ?"
A little PureBasic review
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: [Solved][4.60 RC1] ATan2() parameters are switched

Post by buddymatkona »

It is things like this that make the difference between a standard math library and a product specific library. An accumulation of things like this can change perception of the user interface from "intuitive and friendly" to "quirky and nonintuitive".
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: [Solved][4.60 RC1] ATan2() parameters are switched

Post by Little John »

Well, e.g. Mathematica uses ArcTan[x, y]. So PureBasic is not exactly in bad company. ;)

Regards, Little John
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: [Solved][4.60 RC1] ATan2() parameters are switched

Post by buddymatkona »

@ Little John
I guess my perception of a "standard" math lib comes from early FORTRAN and then C. It seemed like a software developer path of least resistance kinda thing rather than technical authority. :)
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: [Solved][4.60 RC1] ATan2() parameters are switched

Post by MachineCode »

infratec wrote:ATan2(0.0705, 0.9975) should return something arround 0.070559
But the current implementation results in 1.500237
So easily fixed with a quick macro:

Code: Select all

Macro MyATan2(y,x)
  ATan2(x,y)
EndMacro

Debug ATan2(0.0705, 0.9975) ; Returns 1.50023696...
Debug MyATan2(0.0705, 0.9975) ; Returns 0.070559...
Then, when Fred changes it, just delete the macro and search/replace your source and all is well.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Helle
Enthusiast
Enthusiast
Posts: 178
Joined: Wed Apr 12, 2006 7:59 pm
Location: Germany
Contact:

Re: [Solved][4.60 RC1] ATan2() parameters are switched

Post by Helle »

Or you use your own ATan2-procedures:

Code: Select all

Procedure.d ATan2D(y.d, x.d)
  !fld qword[p.v_y]
  !fld qword[p.v_x]
  !fpatan
 ProcedureReturn 
EndProcedure

Procedure.f ATan2F(y.f, x.f)
  !fld dword[p.v_y]
  !fld dword[p.v_x]
  !fpatan
 ProcedureReturn 
EndProcedure

MessageRequester("ATan2 with (y,x), double", StrD(ATan2D(0.0705, 0.9975)))

MessageRequester("ATan2 with (y,x), float", StrF(ATan2F(0.0705, 0.9975)))
Helle
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: [Solved][4.60 RC1] ATan2() parameters are switched

Post by buddymatkona »

I did it a slightly different, lazy way...trusting PB conversions. The FPU version eliminates a lot of overhead so I will use it. Thanks Helle.

Code: Select all

Procedure.d MyATan2(y.d, x.d) 
ProcedureReturn ATan(y / x)
EndProcedure

MessageRequester("ATan2(y,x)", "Float   " + StrF(MyATan2(0.0705, 0.9975)) + Chr(10) + "Double "+  StrD(MyATan2(0.0705, 0.9975)) )
DarkDragon
Addict
Addict
Posts: 2228
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: [Solved][4.60 RC1] ATan2() parameters are switched

Post by DarkDragon »

atan2(y, x) is not equal to atan(y / x) in all cases!
http://en.wikipedia.org/wiki/Atan2#Illustrations
bye,
Daniel
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: [Solved][4.60 RC1] ATan2() parameters are switched

Post by buddymatkona »

I forgot how messy Atan2 was. :oops: Anyway if you need a little more overhead than a macro provides:

Code: Select all

Procedure.d MyATan2(y.d, x.d) 
ProcedureReturn ATan2(x , y)
EndProcedure

MessageRequester("ATan2(y,x)", "Float   " + StrF(MyATan2(0.0705, 0.9975)) + Chr(10) + "Double "+  StrD(MyATan2(0.0705, 0.9975)) )
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: [Solved][4.60 RC1] ATan2() parameters are switched

Post by MachineCode »

@buddymatkona: Don't forget that procedures are always slower than macros, due to the preparation they use when entering the procedure. Macros don't have to do that. In the problem given in this thread, a macro is the correct technique to use. Here's an example showing the speed difference when performing 10000000 ATan2 calculations on my PC. As you can see, the procedure version is far slower due to the preparation involved.

Code: Select all

Macro MyATan2(y,x)
  ATan2(x,y)
EndMacro

Procedure MyATan2P(y,x)
  ProcedureReturn ATan2(x,y)
EndProcedure

DisableDebugger

s=ElapsedMilliseconds()
For n=1 To 10000000
  r=MyATan2(0.0705, 0.9975)
Next
m=ElapsedMilliseconds()-s

s=ElapsedMilliseconds()
For n=1 To 10000000
  r=MyATan2P(0.0705, 0.9975)
Next
p=ElapsedMilliseconds()-s

EnableDebugger

Debug m ; Macro took 906 ms (less than 1 second)
Debug p ; Procedure took 2719 ms (almost 3 seconds)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: [Solved][4.60 RC1] ATan2() parameters are switched

Post by buddymatkona »

@MachineCode
Helle and I left out the quadrant checks and you missed my bad overhead joke. Everybody must have been half asleep that night. :)
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: [Solved][4.60 RC1] ATan2() parameters are switched

Post by MachineCode »

buddymatkona wrote:you missed my bad overhead joke
<Checks> Oops, I did indeed! :oops: LOL, oh well.

Anyway, the Big Question now is: should we remove the [Solved] tag from the subject, and let Fred swap the order of parameters to match the majority of other languages (C, Java, Ruby, Python, PHP, JavaScript)?

I vote yes.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Post Reply