Page 1 of 2
[Solved][4.60 RC1] ATan2() parameters are switched
Posted: Fri Oct 14, 2011 12:58 pm
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
Re: [4.60 RC1] ATan2() parameters are switched
Posted: Fri Oct 14, 2011 1:12 pm
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.
Re: [4.60 RC1] ATan2() parameters are switched
Posted: Fri Oct 14, 2011 1:16 pm
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

Re: [4.60 RC1] ATan2() parameters are switched
Posted: Fri Oct 14, 2011 2:12 pm
by luis
infratec wrote:
P.S.: Oh I found it 4 seconds to late

4 minutes

Re: [Solved][4.60 RC1] ATan2() parameters are switched
Posted: Fri Oct 14, 2011 2:45 pm
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".
Re: [Solved][4.60 RC1] ATan2() parameters are switched
Posted: Fri Oct 14, 2011 4:26 pm
by Little John
Well, e.g.
Mathematica uses
ArcTan[x, y]. So PureBasic is not exactly in bad company.
Regards, Little John
Re: [Solved][4.60 RC1] ATan2() parameters are switched
Posted: Sat Oct 15, 2011 12:47 am
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.

Re: [Solved][4.60 RC1] ATan2() parameters are switched
Posted: Sat Oct 15, 2011 4:54 am
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.
Re: [Solved][4.60 RC1] ATan2() parameters are switched
Posted: Sat Oct 15, 2011 10:36 am
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
Re: [Solved][4.60 RC1] ATan2() parameters are switched
Posted: Sat Oct 15, 2011 6:22 pm
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)) )
Re: [Solved][4.60 RC1] ATan2() parameters are switched
Posted: Sat Oct 15, 2011 6:29 pm
by DarkDragon
atan2(y, x) is not equal to atan(y / x) in all cases!
http://en.wikipedia.org/wiki/Atan2#Illustrations
Re: [Solved][4.60 RC1] ATan2() parameters are switched
Posted: Sat Oct 15, 2011 8:22 pm
by buddymatkona
I forgot how messy Atan2 was.

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)) )
Re: [Solved][4.60 RC1] ATan2() parameters are switched
Posted: Sun Oct 16, 2011 12:43 am
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)
Re: [Solved][4.60 RC1] ATan2() parameters are switched
Posted: Sun Oct 16, 2011 4:43 am
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.

Re: [Solved][4.60 RC1] ATan2() parameters are switched
Posted: Sun Oct 16, 2011 7:45 am
by MachineCode
buddymatkona wrote:you missed my bad overhead joke
<Checks> Oops, I did indeed!

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.