[0.14.7] math.atan error

Things that has been reported already before.
MrDoomah
Fast Inserter
Fast Inserter
Posts: 196
Joined: Mon Jun 01, 2015 1:11 pm
Contact:

[0.14.7] math.atan error

Post by MrDoomah »

In factorio the lua function math.atan behaves different from the normal lua behaviour:

Image

When supplying math.atan(x/y) it does have the regular behaviour, but then you can't get the complete anlge as specified by the lua documentation:
math.atan
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.

Code: Select all

    > c, s = math.cos(0.8), math.sin(0.8)
    > = math.atan(s/c)
    0.8
    > = math.atan(s,c)
    0.8
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
it seems that factorio ignores the second input of atan and always return atan(x).
MrDoomah
Fast Inserter
Fast Inserter
Posts: 196
Joined: Mon Jun 01, 2015 1:11 pm
Contact:

Re: [0.14.7] math.atan error

Post by MrDoomah »

Ok np. I'll just use atan2 function then.

Script for those having the same problem and do know to google factorio + atan before making bug reports.

Code: Select all

function atan2(pos)	--https://en.wikipedia.org/wiki/Atan2
	local phi = 0	-- set this to 0/0 to return NaN when input is {0,0}
	if pos.x > 0 then phi = math.atan(pos.y/pos.x)
	elseif pos.x < 0 then
		if pos.y >= 0 then phi = math.atan(pos.y/pos.x) + math.pi
		else phi = math.atan(pos.y/pos.x) - math.pi end
	else
		if pos.y > 0 then phi = math.pi/2
		elseif pos.y < 0 then phi = -math.pi/2
		end
	end
	return phi + math.pi	-- returns (0,2pi]
end
Post Reply

Return to “Duplicates”