add log10 function
authorLaurent Mazet <mazet@softndesign.org>
Thu, 2 Feb 2023 22:23:15 +0000 (23:23 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Thu, 2 Feb 2023 22:23:15 +0000 (23:23 +0100)
calc.c
parser.c
parser.h

diff --git a/calc.c b/calc.c
index 808aff56cb1cf1ff37ce5238363c7ab2a3cc12cf..2e388611c3d5bad387465fab09ed23324653c52b 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -270,7 +270,8 @@ int main (int argc, char *argv[])
 // test: echo "sin (2)" | calc.exe | grep -q '=> 0\.909297'
 // test: echo "atan (2)" | calc.exe | grep -q '=> 1\.10715'
 // test: echo "exp (2)" | calc.exe | grep -q '=> 7\.38906'
-// test: echo "log (2)" | calc.exe | grep -q '=> 0\.693147'
+// test: echo "ln (2)" | calc.exe | grep -q '=> 0\.693147'
+// test: echo "log (10)" | calc.exe | grep -q '=> 1'
 // test: echo "2 + 3 - 4" | calc.exe | grep -q '=> 1'
 // test: echo "1 + cos (2 - 3)" | calc.exe | grep -q '=> 1\.5403'
 // test: echo "cos (1 / 2) * 3" | calc.exe | grep -q '=> 2\.63275'
@@ -310,8 +311,8 @@ int main (int argc, char *argv[])
 // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe -n | grep -q 2
 // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe | grep -q 64
 // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe | grep -q 2
-// test: echo -e '-cos (1)\n1 + 1\n1 - 1\n1 * 1\n1 / 1\n3%2\n2^2\nsqrt (2)\ncos (0)\nsin (0)\ntan (0)\nacos (0)\nasin (0)\natan (0)\nlog (1)\nexp (1)\nabs (-1)\nceil (1.2)\nfloor (-1.2)\nans\ne\npi\nsto (1)\nrcl (2)\ndisp\nhelp\nquit' | calc.exe -n -v 3 | grep -q bye
-// test: echo -e '1 +\n1 -\n1 * 1\n1 /\n3%\n2^\nsqrt ()\ncos ()\nsin ()\ntan ()\nacos ()\nasin ()\natan ()\nlog ()\nexp ()\nabs ()\nceil ()\nfloor ()\n1 + (\n1+2(\n1+2cos\n1+2pi' | calc.exe | grep -c error | xargs test 21 =
+// test: echo -e '-cos (1)\n1 + 1\n1 - 1\n1 * 1\n1 / 1\n3%2\n2^2\nsqrt (2)\ncos (0)\nsin (0)\ntan (0)\nacos (0)\nasin (0)\natan (0)\nln (1)\nlog (1)\nexp (1)\nabs (-1)\nceil (1.2)\nfloor (-1.2)\nans\ne\npi\nsto (1)\nrcl (2)\ndisp\nhelp\nquit' | calc.exe -n -v 3 | grep -q bye
+// test: echo -e '1 +\n1 -\n1 * 1\n1 /\n3%\n2^\nsqrt ()\ncos ()\nsin ()\ntan ()\nacos ()\nasin ()\natan ()\nln ()\nlog ()\nexp ()\nabs ()\nceil ()\nfloor ()\n1 + (\n1+2(\n1+2cos\n1+2pi' | calc.exe | grep -c error | xargs test 22 =
 // test: echo -e '1 + 1\nans' | calc.exe -p 3 | grep -c 2 | xargs test 2 =
 // test: echo -e 'sin (pi / 2)' | calc.exe -p 4 | grep -q 1
 // test: echo -e 'e ^ 2' | calc.exe | grep -q '7\.38906'
@@ -350,9 +351,9 @@ int main (int argc, char *argv[])
 // test: echo -e 'cos (quit)' | calc.exe | grep -q error
 // test: echo -e '(quit)' | calc.exe | grep -q error
 // test: echo -e 'cos 3.14\n!\n! 3 4' | calc.exe | grep -c error | xargs test 3 =
-// test: echo -e 'sto (2, 3)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe  | grep -q '=> 4\.15888'
-// test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe  | grep -q '=> 1808\.04'
-// test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64))' | calc.exe  | grep -q '=> 0'
+// test: echo -e 'sto (2, 3)\ncond (rcl (2) > 2, ln (64), exp (75 / 10))' | calc.exe  | grep -q '=> 4\.15888'
+// test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, ln (64), exp (75 / 10))' | calc.exe  | grep -q '=> 1808\.04'
+// test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, ln (64))' | calc.exe  | grep -q '=> 0'
 // test: echo -e 'cond (0, 1, 2)\nquit' | calc.exe -v 3 | grep -q Cond
 // test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe | grep -c error | xargs test 3 =
 // test: echo -e 'sto (1, 4)\ninc (1)\ninc (1)\ndec (1)\ninc (1)\nrcl (1) == 6\nquit' | calc.exe -v 3 | grep -q '=> 1'
index aecaae3d3b3a9bcefc7db4e39a93e397cbbaad0b..e20427cb0135215912ae9498ee4a5bb8e7f0c18d 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -130,7 +130,7 @@ keyword_t operators[NB_OPERATORS] = {
     { "|",   Or, 2, 1, -2}
 };
 
-#define NB_FUNCTIONS 31
+#define NB_FUNCTIONS 32
 keyword_t functions[NB_FUNCTIONS] = {
     { "sqrt", Sqr, 1, 4, 5},
     { "pow",  Pow, 2, 3, 5},
@@ -140,6 +140,7 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "acos", Acos, 1, 4, 5},
     { "asin", Asin, 1, 4, 5},
     { "atan", Atan, 1, 4, 5},
+    { "ln",   Ln, 1, 2, 5},
     { "log",  Log, 1, 3, 5},
     { "exp",  Exp, 1, 3, 5},
     { "abs",  Abs, 1, 3, 5},
@@ -513,7 +514,8 @@ void print_element (element_t *root, int level)
     case Acos: func = "Arc Cosine"; break;
     case Asin: func = "Arc Sine"; break;
     case Atan: func = "Arc Tangent"; break;
-    case Log: func = "Logarithm"; break;
+    case Ln: func = "Logarithm (e base)"; break;
+    case Log: func = "Logarithm (10 base)"; break;
     case Exp: func = "Exponantial"; break;
     case Abs: func = "Absolute value"; break;
     case Ceil: func = "Ceil value"; break;
@@ -1050,6 +1052,7 @@ double evaluate_element (element_t *root, char mask)
     case Acos:
     case Asin:
     case Atan:
+    case Ln:
     case Log:
     case Exp:
     case Abs:
@@ -1108,7 +1111,8 @@ double evaluate_element (element_t *root, char mask)
     case Acos: return acos (op0);
     case Asin: return asin (op0);
     case Atan: return atan (op0);
-    case Log: return log (op0);
+    case Ln: return log (op0);
+    case Log: return log10 (op0);
     case Exp: return exp (op0);
     case Abs: return fabs (op0);
     case Ceil: return ceil (op0);
index 0b9233fa406a25834e6cb93c8acea5cc505f7272..dc75776deaef52359a695b7627afe71cfde344da 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -13,7 +13,7 @@ typedef enum {
     Mul, Div, Mod,
     Pow, Sqr,
     Cos, Sin, Tan, Acos, Asin, Atan,
-    Log, Exp,
+    Ln, Log, Exp,
     Abs, Ceil, Floor,
     Store, Recall, Inc, Dec, Disp, Mem, Clear,
     Quit, Help,