Page 1 of 1

Math.atan gives different values?

Posted: Thu Apr 17, 2025 8:49 am
by nmttmn
print(math.atan(1,0))
local i = 1
function Test()
if i <= 1 then
Salvar(math.atan(1,0),'OO.json')
end
end
Test()

function update()
Test()

end

So math.atan gives the correct value: 1.5707963267949, until i run love2d and I call math.atan which will give me 0.78539816339745. Can someone help me understand? Should i try to make my own math.atan?

Re: Math.atan gives different values?

Posted: Thu Apr 17, 2025 9:10 am
by MrFariator
I believe math.atan and math.atan2 were combined to a single function in lua 5.3. LÖVE uses luajit, which is an offshoot of 5.1, with some stuff taken from 5.2. As such, what you're probably after is math.atan2:

Code: Select all

print(math.atan(1)) -- prints: 0.78539816339745
print(math.atan2(1,0)) -- prints: 1.5707963267949
If you try to pass multiple arguments to math.atan in luajit, only the first argument is considered. If you need to be able to run your code in both stock lua and LÖVE, you could monkeypatch math.atan to check the number of arguments:

Code: Select all

if math.atan2 then
  local atan, atan2 = math.atan, math.atan2
  math.atan = function ( arg1, arg2 )
    return arg2 and atan2(arg1,arg2) or atan(arg1)
  end
end

print(math.atan ( 1 )) -- prints: 0.78539816339745
print(math.atan ( 1, 0 )) -- prints: 1.5707963267949