[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: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
matgarage
Posts: 64
Joined: Sat Apr 20, 2013 11:39 am
Location: France

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

Post by matgarage » Sun Oct 08, 2017 2:17 pm

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: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

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

Post by mrcoollion » Sun Oct 08, 2017 7:52 pm

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
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

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

Post by [-hh] » Sun Oct 08, 2017 8:27 pm

Hi.

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

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
Livecode Opensource Backer
Posts: 9358
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

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

Post by richmond62 » Sun Oct 08, 2017 8:58 pm

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


8)

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

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

Post by richmond62 » Sun Oct 08, 2017 9:15 pm

atan2_1.png
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
Livecode Opensource Backer
Posts: 9358
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

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

Post by richmond62 » Sun Oct 08, 2017 9:20 pm

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: 64
Joined: Sat Apr 20, 2013 11:39 am
Location: France

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

Post by matgarage » Mon Oct 09, 2017 5:27 pm

Nice!
Interchanging values in the ATAN2 arguments work perfectly.

Thank you guys

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”