From f801e648bb3ee9e0c49958f8d49487e4b1af407c Mon Sep 17 00:00:00 2001 From: Ian Farmer Date: Sun, 23 Sep 2007 03:54:06 +0000 Subject: [PATCH] New math.cos and math.sqrt functions from Lua standard library, with new math library test case. Also, character encoding of LuaCompat was changed from something that Eclipse was not handling well to UTF-8. --- .../java/lua/addon/luacompat/LuaCompat.java | 30 ++++++++++++------- src/test/res/mathlib.lua | 4 +++ 2 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 src/test/res/mathlib.lua diff --git a/src/addon/java/lua/addon/luacompat/LuaCompat.java b/src/addon/java/lua/addon/luacompat/LuaCompat.java index 1b38b311..7378a359 100644 --- a/src/addon/java/lua/addon/luacompat/LuaCompat.java +++ b/src/addon/java/lua/addon/luacompat/LuaCompat.java @@ -82,10 +82,12 @@ public class LuaCompat extends LFunction { public static final String[] MATH_NAMES = { "abs", + "cos", "max", "min", "modf", - "sin" + "sin", + "sqrt" }; public static final String[] STRING_NAMES = { @@ -138,10 +140,12 @@ public class LuaCompat extends LFunction { private static final int MATH_BASE = 20; private static final int ABS = MATH_BASE + 0; - private static final int MAX = MATH_BASE + 1; - private static final int MIN = MATH_BASE + 2; - private static final int MODF = MATH_BASE + 3; - private static final int SIN = MATH_BASE + 4; + private static final int COS = MATH_BASE + 1; + private static final int MAX = MATH_BASE + 2; + private static final int MIN = MATH_BASE + 3; + private static final int MODF = MATH_BASE + 4; + private static final int SIN = MATH_BASE + 5; + private static final int SQRT = MATH_BASE + 6; private static final int STRING_BASE = 30; private static final int BYTE = STRING_BASE + 0; @@ -244,6 +248,9 @@ public class LuaCompat extends LFunction { case ABS: vm.setResult( abs( vm.getArg( 0 ) ) ); break; + case COS: + vm.setResult( new LDouble( Math.cos ( vm.getArgAsDouble( 0 ) ) ) ); + break; case MAX: vm.setResult( max( vm.getArg( 0 ), vm.getArg( 1 ) ) ); break; @@ -256,7 +263,10 @@ public class LuaCompat extends LFunction { case SIN: vm.setResult( new LDouble( Math.sin( vm.getArgAsDouble( 0 ) ) ) ); break; - + case SQRT: + vm.setResult( new LDouble( Math.sqrt( vm.getArgAsDouble( 0 ) ) ) ); + break; + // String functions case BYTE: StrLib.byte_( vm ); @@ -549,10 +559,10 @@ public class LuaCompat extends LFunction { /** unpack (list [, i [, j]]) * * Returns the elements from the given table. This function is equivalent to - * return list[i], list[i+1], ···, list[j] + * return list[i], list[i+1], ···, list[j] * * except that the above code can be written only for a fixed number of elements. - * By default, i is 1 and j is the length of the list, as defined by the length operator (see §2.5.5). + * By default, i is 1 and j is the length of the list, as defined by the length operator (see §2.5.5). */ private void unpack(VM vm) { LValue v = vm.getArg(0); @@ -636,7 +646,7 @@ public class LuaCompat extends LFunction { // ============= tables support ============= /** table.concat (table [, sep [, i [, j]]]) * - * Given an array where all elements are strings or numbers, returns table[i]..sep..table[i+1] ··· sep..table[j]. + * Given an array where all elements are strings or numbers, returns table[i]..sep..table[i+1] ··· sep..table[j]. * The default value for sep is the empty string, the default for i is 1, and the default for j is the length of the table. * If i is greater than j, returns the empty string. */ @@ -667,7 +677,7 @@ public class LuaCompat extends LFunction { /** table.insert (table, [pos,] value) * * Inserts element value at position pos in table, shifting up other elements to open space, if necessary. - * The default value for pos is n+1, where n is the length of the table (see §2.5.5), so that a call + * The default value for pos is n+1, where n is the length of the table (see §2.5.5), so that a call * table.insert(t,x) inserts x at the end of table t. */ private void insert(VM vm) { diff --git a/src/test/res/mathlib.lua b/src/test/res/mathlib.lua new file mode 100644 index 00000000..ce828d10 --- /dev/null +++ b/src/test/res/mathlib.lua @@ -0,0 +1,4 @@ +print( math.sin( 0.0 ) ) +print( math.cos( math.pi ) ) +print( math.sqrt( 9.0 ) ) +print( math.modf( 5.25 ) )