Skip to content

Commit

Permalink
Add support for Lua 5.3's multi-arg math.atan
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidDev committed Sep 22, 2024
1 parent 1b4ade2 commit 8e9f369
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/main/java/org/squiddev/cobalt/lib/MathLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static void add(LuaState state, LuaTable env) throws LuaError {
RegisteredFunction.of("tan", (s, arg) -> valueOf(Math.tan(arg.checkDouble()))),
RegisteredFunction.of("acos", (s, arg) -> valueOf(Math.acos(arg.checkDouble()))),
RegisteredFunction.of("asin", (s, arg) -> valueOf(Math.asin(arg.checkDouble()))),
RegisteredFunction.of("atan", (s, arg) -> valueOf(Math.atan(arg.checkDouble()))),
RegisteredFunction.of("atan", MathLib::atan),
RegisteredFunction.of("cosh", (s, arg) -> valueOf(Math.cosh(arg.checkDouble()))),
RegisteredFunction.of("log10", (s, arg) -> valueOf(Math.log10(arg.checkDouble()))),
RegisteredFunction.of("sinh", (s, arg) -> valueOf(Math.sinh(arg.checkDouble()))),
Expand Down Expand Up @@ -116,6 +116,14 @@ private static LuaValue log(LuaState state, LuaValue arg1, LuaValue arg2) throws
}
}

private static LuaValue atan(LuaState state, LuaValue arg1, LuaValue arg2) throws LuaError {
if (arg2.isNil()) {
return valueOf(Math.atan(arg1.checkDouble()));
} else {
return valueOf(Math.atan2(arg1.checkDouble(), arg2.checkDouble()));
}
}

private static Varargs frexp(LuaState state, Varargs args) throws LuaError {
double x = args.arg(1).checkDouble();
if (x == 0) return varargsOf(Constants.ZERO, Constants.ZERO);
Expand Down
6 changes: 6 additions & 0 deletions src/test/resources/spec/math_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ describe("The math library", function()
expect.error(math.random, 1, 2, 3):eq("wrong number of arguments")
end)
end)

describe("math.atan", function()
it("accepts two arguments :lua>=5.3", function()
expect(math.atan(0.5, 0.5)):eq(math.pi / 4)
end)
end)
end)

0 comments on commit 8e9f369

Please sign in to comment.