Skip to content

math.atan does not support 2 argument version #328

Description

@The3DPrintist

I believe the math.atan function is incorrectly calculating (or not considering) the two argument version of math.atan(y,x).

I was able to restore functionality for my project using this replacement but this is probably not optimized, wanted to post this here so others could use it since its pretty common in game-dev 2D/3D vector math:

  local oldatan = math.atan
  function math.atan(y,x)
      if(x==nil)then return oldatan(y) end
      if(x>0)then return oldatan(y/x) end
      if(x<0 and y >= 0)then return oldatan(y/x) + math.pi end
      if(x<0 and y < 0)then return oldatan(y/x) - math.pi end
      if(x==0 and y>0)then return math.pi/2 end
      if(x==0 and y<0)then return -math.pi/2 end
      return nil
  end

  print(math.atan(1)," 0.78539816339745 (π/4)")
  print(math.atan(0)," 0")
  print(math.atan(-1)," -0.78539816339745 (-π/4)")
  
  print(math.atan(1, 1)," 0.78539816339745 (π/4)")
  print(math.atan(1, 0)," 1.5707963267949 (π/2)")
  print(math.atan(-1, 0)," -1.5707963267949 (-π/2)")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions