diff --git a/docs/math.md b/docs/math.md index acb3f01..033c54f 100644 --- a/docs/math.md +++ b/docs/math.md @@ -10,7 +10,7 @@ Same as in C++. | 1 | `%` `*` `/` | | 2 | `+` `-` | | 3 | bit shifts (NI) | -| 4 | `<=>` (NI) | +| 4 | `<=>` (1) | | 5 | `<=` `>=` `<` `>` | | 6 | `==` `!=` | | 7 | bitwise and `&` (NI) | @@ -18,7 +18,11 @@ Same as in C++. | 10 | `&&` | | 11
lowest | `\|\|` | -- (NI) stands for not yet implemented. +(NI) +: not yet implemented. + +(1) +: Shuttle operator: `a <=> b` returns 0 if a == b, negative Int if a < b and positive Int otherwise. It is necessary to override shuttle operator to make a class comparable. ## Operators @@ -43,22 +47,39 @@ or transformed `Real` otherwise. | floor(x) | Computes the largest integer value not greater than x | | ceil(x) | Computes the least integer value value not less than x | | round(x) | Rounds x | -| | | +| abs(x) | absolute value, Int for integer x, Real otherwise | | | | -## Scientific functions +## Mathematical functions -| name | meaning | -|---------------------|---------| -| `sin(x:Real): Real` | sine | -| | | -| | | -| | | +| name | meaning | +|-----------|------------------------------------------------------| +| sin(x) | sine | +| cos(x) | cosine | +| tan(x) | tangent | +| asin(x) | $sin^-1(x)$ | +| acos(x) | $cos^-1(x)$ | +| atan(x) | $tg^-1(x)$ | +| sinh(x) | hyperbolic sine | +| cosh(x) | hyperbolic cosine | +| tanh(x) | hyperbolic tangent | +| asinh(x) | $sinh^-1(x)$ | +| acosh(x) | $cosh^-1(x)$ | +| atanh(x) | $tgh^-1(x)$ | +| ln(x) | $ln(x)$, $ log_e(x) $ | +| exp(x) | $e^x$ | +| log10(x) | $log_{10}(x)$ | +| pow(x, y) | ${x^y}$ | +| sqrt(x) | $ \sqrt {x}$ | +| abs(x) | absolute value of x. Int if x is Int, Real otherwise | -For example: - sin(π/2) - >>> 1.0 +For example: + + assert( sin(π/2) == 1.0) + assert( cos(π/2) < 0.000001) + assert( abs(ln(exp(1))) - 1 < 0.00001) + >>> void ## Scientific constant diff --git a/library/src/commonMain/kotlin/net/sergeych/lyng/Script.kt b/library/src/commonMain/kotlin/net/sergeych/lyng/Script.kt index e6be10a..5b019c7 100644 --- a/library/src/commonMain/kotlin/net/sergeych/lyng/Script.kt +++ b/library/src/commonMain/kotlin/net/sergeych/lyng/Script.kt @@ -43,9 +43,75 @@ class Script( (if (x is ObjInt) x else ObjReal(round(x.toDouble()))) } + addFn("sin") { ObjReal(sin(args.firstAndOnly().toDouble())) } + addFn("cos") { + ObjReal(cos(args.firstAndOnly().toDouble())) + } + addFn("tan") { + ObjReal(tan(args.firstAndOnly().toDouble())) + } + addFn("asin") { + ObjReal(asin(args.firstAndOnly().toDouble())) + } + addFn("acos") { + ObjReal(acos(args.firstAndOnly().toDouble())) + } + addFn("atan") { + ObjReal(atan(args.firstAndOnly().toDouble())) + } + + addFn("sinh") { + ObjReal(sinh(args.firstAndOnly().toDouble())) + } + addFn("cosh") { + ObjReal(cosh(args.firstAndOnly().toDouble())) + } + addFn("tanh") { + ObjReal(tanh(args.firstAndOnly().toDouble())) + } + addFn("asinh") { + ObjReal(asinh(args.firstAndOnly().toDouble())) + } + addFn("acosh") { + ObjReal(acosh(args.firstAndOnly().toDouble())) + } + addFn("atanh") { + ObjReal(atanh(args.firstAndOnly().toDouble())) + } + + addFn("exp") { + ObjReal(exp(args.firstAndOnly().toDouble())) + } + addFn("ln") { + ObjReal(ln(args.firstAndOnly().toDouble())) + } + + addFn("log10") { + ObjReal(log10(args.firstAndOnly().toDouble())) + } + + addFn("log2") { + ObjReal(log2(args.firstAndOnly().toDouble())) + } + + addFn("pow") { + requireExactCount(2) + ObjReal( + (args[0].toDouble()).pow(args[1].toDouble()) + ) + } + addFn("sqrt") { + ObjReal( + sqrt(args.firstAndOnly().toDouble()) + ) + } + addFn( "abs" ) { + val x = args.firstAndOnly() + if( x is ObjInt ) ObjInt( x.value.absoluteValue ) else ObjReal( x.toDouble().absoluteValue ) + } addVoidFn("assert") { val cond = requiredArg(0)