Page 1 of 1

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

Posted: Sun Oct 08, 2017 2:17 pm
by matgarage
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

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

Posted: Sun Oct 08, 2017 7:52 pm
by mrcoollion
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

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

Posted: Sun Oct 08, 2017 8:27 pm
by [-hh]
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).

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

Posted: Sun Oct 08, 2017 8:58 pm
by richmond62
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)

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

Posted: Sun Oct 08, 2017 9:15 pm
by richmond62
atan2_1.png
And, look at this: LibreOffice's "Excel rip-off" CALC uses the opposite order of variable to LiveCode just like Excel.

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

Posted: Sun Oct 08, 2017 9:20 pm
by richmond62
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.

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

Posted: Mon Oct 09, 2017 5:27 pm
by matgarage
Nice!
Interchanging values in the ATAN2 arguments work perfectly.

Thank you guys