tisp

tiny lisp
git clone git://edryd.org/tisp
Log | Files | Refs | LICENSE

commit 3be6d85c774f18872bc2482221862c6211e82948
parent e54d2fd87a003d4cab9a1929c92db8ab9c87a3f8
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Sat,  2 Feb 2019 18:35:26 -0800

If only one number is given to /, take its inverse

Diffstat:
test.c | 2++
tibs/math.c | 11+++++++++--
2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/test.c b/test.c @@ -213,6 +213,8 @@ char *tests[][2] = { { "(/ 8 4)", "2" }, { "(/ 6 2.1)", "2.85714285714286" }, { "(/ 4 4/3)", "3" }, + { "(/ 5)", "1/5" }, + { "(/ 4473)", "1/4473" }, { "(/ 10.42 5)", "2.084" }, { "(/ 1.34e-2 4.3332)", "0.0030924028431644" }, { "(/ 1.04 -15/4)", "-0.277333333333333" }, diff --git a/tibs/math.c b/tibs/math.c @@ -116,9 +116,16 @@ static Val prim_div(Env env, Val args) { Val a, b; - tsp_arg_num(args, "/", 2); + int len = list_len(args); + if (len != 2 && len != 1) + tsp_warnf("/: expected 1 or 2 arguments, recieved %d", len); EVAL_CHECK(a, car(args), "/", NUMBER); - EVAL_CHECK(b, car(cdr(args)), "/", NUMBER); + if (len == 1) { + b = a; + a = mk_int(1); + } else { + EVAL_CHECK(b, car(cdr(args)), "/", NUMBER); + } if (a->t & DECIMAL || b->t & DECIMAL) return mk_dub((a->v.n.num/a->v.n.den) / (b->v.n.num/b->v.n.den)); return (mk_num(a->t, b->t, 1))