From 0a21f890aeafebc15c4b064707a7ace229262dc2 Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Mon, 8 Jun 2020 10:14:32 +0700 Subject: [PATCH] add more math functions --- src/Lib/Math.php | 56 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/src/Lib/Math.php b/src/Lib/Math.php index a8ddba0..72d9f50 100644 --- a/src/Lib/Math.php +++ b/src/Lib/Math.php @@ -10,15 +10,19 @@ class Math implements ILib { // Basic arithmetic - $env->set('+', new CoreFunc('+', 'Return the sum of all arguments.', 2, -1, + $env->set('+', new CoreFunc('+', 'Return the sum of all arguments.', 1, -1, function (...$args) { return array_sum($args); } )); - $env->set('-', new CoreFunc('-', 'Subtract the other arguments from the first.', 2, -1, + $env->set('-', new CoreFunc('-', 'Subtract the other arguments from the first.', 1, -1, function (...$args) { - return array_reduce(array_slice($args, 1), fn ($a, $b) => $a - $b, $args[0]); + if (count($args) == 1) { + return -$args[0]; + } else { + return array_reduce(array_slice($args, 1), fn ($a, $b) => $a - $b, $args[0]); + } } )); @@ -46,6 +50,50 @@ class Math implements ILib } )); - // TODO: add pow, sqrt, floor, ceil, abs + // Helpers for change by 1 + + $env->set('inc', new CoreFunc('inc', 'Increase argument by one.', 1, 1, + fn ($a) => $a + 1 + )); + + $env->set('dec', new CoreFunc('dec', 'Decrease argument by one.', 1, 1, + fn ($a) => $a - 1 + )); + + // Trigonometry + + $env->set('sin', new CoreFunc('sin', 'Calculate the sine of argument.', 1, 1, + fn ($a) => sin($a) + )); + + $env->set('cos', new CoreFunc('cos', 'Calculate the cosine of argument.', 1, 1, + fn ($a) => cos($a) + )); + + $env->set('tan', new CoreFunc('tan', 'Calculate the tangent of argument.', 1, 1, + fn ($a) => tan($a) + )); + + // Other + + $env->set('abs', new CoreFunc('abs', 'Return the absolute value of argument.', 1, 1, + fn ($a) => abs($a) + )); + + $env->set('floor', new CoreFunc('floor', 'Return the next lowest integer by rounding argument down.', 1, 1, + fn ($a) => intval(floor($a)) + )); + + $env->set('ceil', new CoreFunc('ceil', 'Return the next highest integer by rounding argument up.', 1, 1, + fn ($a) => intval(ceil($a)) + )); + + $env->set('pow', new CoreFunc('pow', 'Return the first argument raised to the power of second argument.', 2, 2, + fn ($a, $b) => pow($a, $b) + )); + + $env->set('sqrt', new CoreFunc('sqrt', 'Return the square root of the arguemnt.', 1, 1, + fn ($a) => sqrt($a) + )); } }