fix: calc parses negative numbers

This commit is contained in:
Sebastian Bensusan
2021-07-17 22:48:23 -07:00
committed by Tienson Qin
parent d05c39b534
commit 4c10750fce
3 changed files with 38 additions and 22 deletions

View File

@@ -22,7 +22,12 @@
98123 "9,8,123"
1123.0 " 112,3.0 "
22.1124131 "2,2.1124131"
100.01231 " 1,00.01231 ")))
100.01231 " 1,00.01231 "))
(testing "even when they are negative"
(are [value expr] (= value (run expr))
-98123 "-98123"
-1123.0 " -112,3.0 "
-22.1124131 "-2,2.1124131")))
(testing "basic operations work"
(are [value expr] (= value (run expr))
1 "1 + 0"
@@ -32,11 +37,15 @@
1 "(2-1 ) "
211 "100 + 111"
2111 "1,000 + 11,11"
-111 "1,000 + -11,11"
0 "1 + 2 + 3 + 4 + 5 -1-2-3-4-5"
1 "1 * 1"
-1 "1 * -1"
2 "1*2"
-2 "-1*2"
9 " 3 *3"
1 " 2 * 3 / 3 / 2"
-1 " 2 * 3 / 3 / -2"
#?(:clj 1/2
:cljs 0.5) " 1 / 2"
0.5 " 1/ 2.0"))
@@ -62,6 +71,7 @@
1.0e1 "1.0e01"
1.23e-10 "123.0e-12"
12.3 "123.0e-1"
-12.3 "-123.0e-1"
12.3 "123.0E-1"
2.0 "1e0 + 1e0"))
(testing "scientific functions"
@@ -80,9 +90,11 @@
(calc/eval env (calc/parse expr))
(= final-env @env))
{"a" 1} "a = 1"
{"a" -1} "a = -1"
{"variable" 1} "variable = 1 + 0 * 2"
{"x" 1} "x= 2 * 1 - 1 "
{"y" 4} "y =8 / 4 + 2 * 1 - 25 * 0 / 1"
{"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"