Browse Source

rest of math library

Kajetan Johannes Hammerle 3 years ago
parent
commit
ceb0586560
3 changed files with 69 additions and 0 deletions
  1. 26 0
      libraries/Math.c
  2. 24 0
      tests/system/math
  3. 19 0
      tests/system/math.out

+ 26 - 0
libraries/Math.c

@@ -14,6 +14,20 @@
 FLOAT_FUNCTION(Sin, sinf)
 FLOAT_FUNCTION(Cos, cosf)
 FLOAT_FUNCTION(Tan, tanf)
+FLOAT_FUNCTION(ASin, asinf)
+FLOAT_FUNCTION(ACos, acosf)
+FLOAT_FUNCTION(ATan, atanf)
+FLOAT_FUNCTION(Ln, logf)
+FLOAT_FUNCTION(Log, log10f)
+FLOAT_FUNCTION(SquareRoot, sqrtf)
+
+static void lPow(Script* sc) {
+    float x;
+    float y;
+    if(sPopFloat(sc, &x) && sPopFloat(sc, &y)) {
+        sPushFloat(sc, powf(y, x));
+    }
+}
 
 void lAddFloatFunction(const char* name, ScriptFunction sf) {
     Function f;
@@ -26,4 +40,16 @@ void lMathRegister() {
     lAddFloatFunction("sin", lSin);
     lAddFloatFunction("cos", lCos);
     lAddFloatFunction("tan", lTan);
+    lAddFloatFunction("asin", lASin);
+    lAddFloatFunction("acos", lACos);
+    lAddFloatFunction("atan", lATan);
+    lAddFloatFunction("ln", lLn);
+    lAddFloatFunction("log", lLog);
+    lAddFloatFunction("sqrt", lSquareRoot);
+
+    Function f;
+    gfInit(&f, "pow", dtFloat(), lPow);
+    gfAddArgument(&f, dtFloat());
+    gfAddArgument(&f, dtFloat());
+    gfsAdd(&f);
 }

+ 24 - 0
tests/system/math

@@ -23,4 +23,28 @@ void main() {
     test(abs(tan(0.0 * PI)));
     test(abs(tan(1.0 * PI)));
     test(abs(tan(2.0 * PI)));
+    
+    test(abs(asin(sin(0.0))));
+    test(abs(asin(sin(0.5))));
+    test(abs(asin(sin(1.0))));
+    test(abs(asin(sin(1.5))));
+    
+    test(abs(acos(cos(0.0))));
+    test(abs(acos(cos(0.5))));
+    test(abs(acos(cos(1.0))));
+    test(abs(acos(cos(1.5))));
+    
+    test(abs(atan(tan(0.0))));
+    test(abs(atan(tan(0.5))));
+    test(abs(atan(tan(1.0))));
+    test(abs(atan(tan(1.5))));
+    
+    test(ln(256.0) / ln(2.0));
+    test(log(256.0) / log(2.0));
+    test(log(1000.0));
+    test(sqrt(2.0));
+    test(sqrt(10000.0));
+    
+    test(pow(2.0, 3.0));
+    test(pow(2.0, 0.5));
 }

+ 19 - 0
tests/system/math.out

@@ -11,3 +11,22 @@
 0.00
 0.00
 0.00
+0.00
+0.50
+1.00
+1.50
+0.00
+0.50
+1.00
+1.50
+0.00
+0.50
+1.00
+1.50
+8.00
+8.00
+3.00
+1.41
+100.00
+8.00
+1.41