feat: add Math library with abs, ceil, floor, trig, and xorshift RNG

Implement a new optional Math module for the Wren standard library, gated behind the WREN_USE_LIB_MATH flag (default on). The module provides double-precision math functions (abs, ceil, floor, int, frac, sin, cos, tan, deg, rad) and a xorshift-based pseudo-random number generator with srand/rand methods. Includes comprehensive test files for each function and an all_tests runner.
This commit is contained in:
Marco Lizza
2015-01-22 15:41:19 +00:00
parent a4baa42d6f
commit 9a50b5f318
10 changed files with 206 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
IO.print(Math.abs(123)) // expect: 123
IO.print(Math.abs(-123)) // expect: 123
IO.print(Math.abs(0)) // expect: 0
IO.print(Math.abs(-0)) // expect: 0
IO.print(Math.abs(-0.12)) // expect: 0.12
IO.print(Math.abs(12.34)) // expect: 12.34
IO.print(Math.abs(1.0)) // expect: 1
IO.print(Math.abs(-1.0)) // expect: 1
+40
View File
@@ -0,0 +1,40 @@
IO.print("abs")
IO.print(Math.abs(123)) // expect: 123
IO.print(Math.abs(-123)) // expect: 123
IO.print(Math.abs(0)) // expect: 0
IO.print(Math.abs(-0)) // expect: 0
IO.print(Math.abs(-0.12)) // expect: 0.12
IO.print(Math.abs(12.34)) // expect: 12.34
IO.print(Math.abs(1.0)) // expect: 1
IO.print(Math.abs(-1.0)) // expect: 1
IO.print("ceil")
IO.print(Math.ceil(2.3)) // expect: 3
IO.print(Math.ceil(3.8)) // expect: 4
IO.print(Math.ceil(-2.3)) // expect: -2
IO.print(Math.ceil(-3.8)) // expect: -3
IO.print("floor")
IO.print(Math.floor(2.3)) // expect: 2
IO.print(Math.floor(3.8)) // expect: 3
IO.print(Math.floor(-2.3)) // expect: -3
IO.print(Math.floor(-3.8)) // expect: -4
IO.print("int")
IO.print(Math.int(8)) // expect: 8
IO.print(Math.int(12.34)) // expect: 12
IO.print(Math.int(-8)) // expect: -8
IO.print(Math.int(-12.34)) // expect: -12
IO.print("frac")
IO.print(Math.frac(8)) // expect: 0
IO.print(Math.frac(12.34)) // expect: 0.34
IO.print(Math.frac(-8)) // expect: -0
IO.print(Math.frac(-12.34)) // expect: -0.34
IO.print("srand/rand")
Math.srand
for (i in 1..10) {
IO.print(Math.floor(Math.rand * 100))
}
+4
View File
@@ -0,0 +1,4 @@
IO.print(Math.ceil(2.3)) // expect: 3
IO.print(Math.ceil(3.8)) // expect: 4
IO.print(Math.ceil(-2.3)) // expect: -2
IO.print(Math.ceil(-3.8)) // expect: -3
+4
View File
@@ -0,0 +1,4 @@
IO.print(Math.floor(2.3)) // expect: 2
IO.print(Math.floor(3.8)) // expect: 3
IO.print(Math.floor(-2.3)) // expect: -3
IO.print(Math.floor(-3.8)) // expect: -4
+4
View File
@@ -0,0 +1,4 @@
IO.print(Math.frac(8)) // expect: 0
IO.print(Math.frac(12.34)) // expect: 0.34
IO.print(Math.frac(-8)) // expect: -0
IO.print(Math.frac(-12.34)) // expect: -0.34
+4
View File
@@ -0,0 +1,4 @@
IO.print(Math.int(8)) // expect: 8
IO.print(Math.int(12.34)) // expect: 12
IO.print(Math.int(-8)) // expect: -8
IO.print(Math.int(-12.34)) // expect: -12
+4
View File
@@ -0,0 +1,4 @@
Math.srand
for (i in 1..10) {
IO.print(Math.floor(Math.rand * 100))
}