Page 1 of 1

[0.13.17] math.atan(y,x) missing/broken

Posted: Thu Aug 25, 2016 2:27 pm
by Qon
http://lua-users.org/wiki/MathLibraryTutorial wrote: Return the inverse tangent in radians. We can do this by supplying y/x ourselves or we can pass y and x to math.atan to do this for us.
[...]
Using two arguments should usually be preferred, particularly when converting rectangular co-ordinates to polar co-ordinates. It will use the sign of both arguments to place the result into the correct quadrant, and also produces correct values when one of its arguments is 0 or very close to 0.

Code: Select all

    > = math.atan(1, 0), math.atan(-1, 0), math.atan(0, 1), math.atan(0, -1)
    1.5707963267949 -1.5707963267949        0        3.1415926535898
I don't get these return values at all. math.atan(1,0) gives me pi/4 (instead of pi/2) and rotating towards (0,-1) decreases the value towards 0 instead of increasing.
I tried:

Code: Select all

local dir = game.tick%360
p.print('test direction('..dir..', '..math.deg(math.atan(math.sin(math.rad(dir)),math.cos(math.rad(dir))))..')')
But it gives the same result as:

Code: Select all

p.print('test direction('..dir..', '..math.deg(math.atan(math.sin(math.rad(dir)),1354586313153))..')')
math.tan(y/x) gives expected values though.
So I tried:

Code: Select all

p.print('test direction('..dir..', '..math.deg(math.atan(math.sin(math.rad(dir))/math.cos(math.rad(dir)),564863154))..')')
Same result as the single argument version.

So it's just calling math.tan(a) instead of math.tan(a,b)
I can work with that, but it's been such a bother finding out that it doesn't exist and wondering why my code doesn't work.

Found official documentation for math.atan(y,x) also.
http://www.lua.org/manual/5.3/manual.html#pdf-math.atan wrote: math.atan (y [, x])

Returns the arc tangent of y/x (in radians), but uses the signs of both parameters to find the quadrant of the result. (It also handles correctly the case of x being zero.)

The default value for x is 1, so that the call math.atan(y) returns the arc tangent of y.
Might be worth checking that other functions like math.log (x [, base]) and similar are correct.

Re: [0.13.17] math.atan(y,x) missing/broken

Posted: Thu Aug 25, 2016 3:01 pm
by Oxyd
That wiki page you've been quoting seems to describe Lua 5.3, but we use Lua 5.2. And, as you can find out in the official documentation for Lua 5.2, math.atan only takes a single argument. math.atan2 is the 2-argument version you've been looking for.

Not a bug.

Re: [0.13.17] math.atan(y,x) missing/broken

Posted: Thu Aug 25, 2016 3:07 pm
by Qon
Ouch, facepalm. Sorry :roll:

I should have checked that first...