Math.atan gives different values?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
nmttmn
Prole
Posts: 4
Joined: Sun Mar 30, 2025 7:42 am

Math.atan gives different values?

Post 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?
MrFariator
Party member
Posts: 578
Joined: Wed Oct 05, 2016 11:53 am

Re: Math.atan gives different values?

Post 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
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 12 guests