Page 2 of 2

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

Posted: Sun Oct 16, 2011 12:00 pm
by DarkDragon
MachineCode wrote:let Fred swap the order of parameters to match the majority of other languages (C, Java, Ruby, Python, PHP, JavaScript)?
No.

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

Posted: Sun Oct 16, 2011 9:31 pm
by freak
I actually put some thought into the parameter order here. This was not an accident, so it won't change.

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

Posted: Mon Oct 17, 2011 12:55 am
by Shield
Well to me, it actually makes more sense.
I'm not a math god so I'd need to look it up anyway, but:
it's really hard to find another situation where the y argument comes before the x...it just doesn't happen. :)

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

Posted: Mon Oct 17, 2011 6:43 am
by DarkDragon
Shield wrote:Well to me, it actually makes more sense.
I'm not a math god so I'd need to look it up anyway, but:
it's really hard to find another situation where the y argument comes before the x...it just doesn't happen. :)
I'm sure the first intention of the atan2(y, x) command was to stay consistent with atan(y / x) ;-) , but you already said it: there is also a reason for atan2(x, y) because no other command ever uses the antialphabetical order when it comes to 2d coordinates.

+1 -1 = 0, neutral position = don't do anything, as it wouldn't be worth to.
But there are already codes which use atan2(x, y), so the reasons to keep it this way are more.

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

Posted: Mon Oct 17, 2011 7:13 am
by infratec
Hi,

in general it is no problem.
If I write a new program I'm so unsure, that I look into the help.
But... as it happens to me:

If you translate a program from an other language to PB I'm 100% sure that 90% runs in this trouble.
(like me :( )
And to find a fault in a mathematical program is really hard.

Bernd

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

Posted: Mon Oct 17, 2011 7:57 am
by Lord
I think, it should be like it is now.
Tangens is definde Y divided by X in the
unit circle, so it is logically for ATAN2()
to first get Y and then X as argument.
By the way: if you look at Wikipedia, there
ist always ATN2(y,x) used and also Excel
demands (y,x)-order.

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

Posted: Mon Oct 17, 2011 8:13 pm
by Zach
Just be careful when translating programs or starting new ones of your own..

The context help should clearly lay out the parameter order as well, if you look at the status bar while typing.

If it really bugs people, they can always just write a Macro that takes the parameter order they are used to, and then reverses it internally.

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

Posted: Tue Oct 18, 2011 11:53 am
by infratec
@Zach

Yes, you see the parameters in the status line. That's correct.

But if you have a code like this

Code: Select all

var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var lat1 = lat1.toRad();
var lat2 = lat2.toRad();

var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c;
which you only translate to PB, I'm sure you don't know what is x and what is y.

Bernd

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

Posted: Tue Oct 18, 2011 12:42 pm
by Zach
I would posit that is the fault of the original coder for not properly commenting :|

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

Posted: Tue Oct 18, 2011 12:46 pm
by rsts
infratec wrote:@Zach

Yes, you see the parameters in the status line. That's correct.

But if you have a code like this

Code: Select all

var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var lat1 = lat1.toRad();
var lat2 = lat2.toRad();

var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c;
which you only translate to PB, I'm sure you don't know what is x and what is y.

Bernd
Then you're basically guessing anyway :)

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

Posted: Sat Oct 22, 2011 8:11 pm
by luis
MachineCode wrote:
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.

Another way:

Code: Select all

; Atan2_inverted.pb

Procedure.f _Atan2(x.f,y.f)
 ProcedureReturn ATan2(x,y)
EndProcedure

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

Code: Select all

IncludeFile "Atan2_inverted.pb"

Debug ATan2 (0.0705, 0.9975) ; Returns 0.070559...
This way you can call your private implementation of ATan2() and still invoke it as ATan2().
If PB change the params order, just remove the include and you are set.

The advantage is there is no need to do a search and replace.

Especially useful if you redefine many PB commands at once and to quickly switch back and forth between the different command sets.

I posted this only as an example of a technique that can be useful in some other case, since Freak already stated this probably will not change.