feat: add scientific numbers and variables with underscores

This commit is contained in:
Sebastian Bensusan
2021-06-12 07:08:23 -07:00
committed by Tienson Qin
parent 56de7fbd40
commit f611657bef
3 changed files with 26 additions and 6 deletions

View File

@@ -48,6 +48,13 @@
432.0 "(3*2) ^ (2 + 1) * 2"
97.0 "(2 * 3) * 2 ^ (2 * 2) + 1"
4.0 "2 * 3 / 2 ^ 2 * 2 + 1"))
(testing "scientific numbers"
(are [value expr] (= value (run expr))
1.0e1 "1.0e01"
1.23e-10 "123.0e-12"
12.3 "123.0e-1"
12.3 "123.0E-1"
2.0 "1e0 + 1e0"))
(testing "scientific functions"
(are [value expr] (= value (run expr))
1.0 "cos( 0 * 1 )"
@@ -69,12 +76,23 @@
{"y" 4} "y =8 / 4 + 2 * 1 - 25 * 0 / 1"
{"zzz" 14.0} "zzz=3 *2 ^ 2 + 1 * 2"
{"foo" 74.0} "foo = (((3*2) ^ 2 + 1) * 2)"))
(testing "variables can have underscores"
(are [final-env expr] (let [env (calc/new-env)]
(calc/eval env (calc/parse expr))
(= final-env @env))
{"a_a" 1} "a_a = 1"
{"a_a_" 1} "a_a_ = 1"
{"_" 1} "_ = 1"
{"__" 1} "__ = 1"
{"foo_bar_baz" 1} "foo_bar_baz = 1 + 0 * 2"
{"foo___bar_baz" 1} "foo___bar_baz = 1 + 0 * 2"))
(testing "variables can be reused"
(are [final-env exprs] (let [env (calc/new-env)]
(doseq [expr exprs]
(calc/eval env (calc/parse expr)))
(= final-env @env))
(doseq [expr exprs]
(calc/eval env (calc/parse expr)))
(= final-env @env))
{"a" 1 "b" 2} ["a = 1" "b = a + 1"]
{"a_a" 1 "b_b" 2} ["a_a = 1" "b_b = a_a + 1"]
{"variable" 1 "x" 0.0} ["variable = 1 + 0 * 2" "x = log(variable)"]
{"x" 1 "u" 23 "v" 24} ["x= 2 * 1 - 1 " "23 + 54" "u= 23" "v = x + u"]))
(testing "variables can be rewritten"