## [SOLVED]Atan2 function give a different result in excel and livecode

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

matgarage
Posts: 58
Joined: Sat Apr 20, 2013 11:39 am

### [SOLVED]Atan2 function give a different result in excel and livecode

Hi,

I'm using atan2 function to perform a calculation in Livecode.

Coud someone explain me why the result of :

Code: Select all

``put atan2(-7.56,65.76) into tResult``
give me -0.114461 in livecode

and
=ATAN2(-7,56;65,76)
give me 1,685257 in excel

Thanks if you could help me
Last edited by matgarage on Mon Oct 09, 2017 5:28 pm, edited 1 time in total.

mrcoollion
Posts: 329
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

### Re: Atan2 function give a different result in excel and livecode

The result of the atan2 function is returned in radians. To get this result in degrees, use the following custom function:
function atan2InDegrees firstArg,secondArg
return atan2(firstArg,secondArg) * 180 / pi
end atan2InDegrees

see http://docs.runrev.com/Function/atan2

regards,

Paul

[-hh]
VIP Livecode Opensource Backer
Posts: 1765
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

### Re: Atan2 function give a different result in excel and livecode

Hi.

The order of the arguments in atan2(x,y) is not unique, be very cautious with that!

Excel atan2(x, y) = LC atan2(y, x) -- interchange x and y

that is, with your example LC atan2(65.76, -7.56) = 1.685257 = Excel atan2(-7.56, 65.76).
shiftLock happens

richmond62
Livecode Opensource Backer
Posts: 2720
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

### Re: Atan2 function give a different result in excel and livecode

This is from the built-in Dictionary:

The result of the atan2 function is returned in radians. To get this result in degrees, use the following custom function:

function atan2InDegrees firstArg,secondArg
return atan2(firstArg,secondArg) * 180 / pi
end atan2InDegrees

richmond62
Livecode Opensource Backer
Posts: 2720
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

### Re: Atan2 function give a different result in excel and livecode

And, look at this: LibreOffice's "Excel rip-off" CALC uses the opposite order of variable to LiveCode just like Excel.

richmond62
Livecode Opensource Backer
Posts: 2720
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

### Re: Atan2 function give a different result in excel and livecode

But, for those who REALLY CARE, while BBC BASIC does NOT implement atan2 one can do this:

10 DEF FNatan2(y,x) : ON ERROR LOCAL = SGN(y)*PI/2
20 IF x>0 THEN = ATN(y/x) ELSE IF y>0 THEN = ATN(y/x)+PI ELSE = ATN(y/x)-PI

which, you will notice, uses the same variable order as LiveCode.

matgarage
Posts: 58
Joined: Sat Apr 20, 2013 11:39 am

### Re: Atan2 function give a different result in excel and livecode

Nice!
Interchanging values in the ATAN2 arguments work perfectly.

Thank you guys