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

Just starting out? Need help? Post your questions and find answers here.
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

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

Post 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.
bye,
Daniel
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

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

Post by freak »

I actually put some thought into the parameter order here. This was not an accident, so it won't change.
quidquid Latine dictum sit altum videtur
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

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

Post 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. :)
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
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

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

Post 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.
bye,
Daniel
infratec
Always Here
Always Here
Posts: 7629
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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
User avatar
Lord
Addict
Addict
Posts: 907
Joined: Tue May 26, 2009 2:11 pm

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

Post 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.
Image
Zach
Addict
Addict
Posts: 1676
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

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

Post 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.
infratec
Always Here
Always Here
Posts: 7629
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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
Zach
Addict
Addict
Posts: 1676
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

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

Post by Zach »

I would posit that is the fault of the original coder for not properly commenting :|
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

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

Post 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 :)
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

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

Post 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.
"Have you tried turning it off and on again ?"
Post Reply