From 3f7c28ed9255ce0332b2e9214ee771c8a1a8dd1c Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 21 Mar 2012 16:47:46 -0700 Subject: * arith.c (divi): New function. * eval.c (eval_init): divi registered as / intrinsic. * lib.h (divi): Declared. * txr.1: divi added to stub heading. * txr.vim: / operator highlighted. --- arith.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'arith.c') diff --git a/arith.c b/arith.c index f888ca6f..87565a7e 100644 --- a/arith.c +++ b/arith.c @@ -880,6 +880,44 @@ divzero: uw_throw(numeric_error_s, lit("mod: division by zero")); } +val divi(val anum, val bnum) +{ + switch (type(anum)) { + case NUM: + case BGNUM: + anum = flo_int(anum); + case FLNUM: + break; + default: + goto type; + } + + switch (type(bnum)) { + case NUM: + case BGNUM: + bnum = flo_int(bnum); + case FLNUM: + break; + default: + goto type; + } + + { + double a = c_flo(anum); + double b = c_flo(bnum); + + if (b == 0.0) + goto divzero; + + return flo(a / b); + } + +divzero: + uw_throw(numeric_error_s, lit("divi: division by zero")); +type: + uw_throwf(error_s, lit("divi: invalid operands ~s ~s"), anum, bnum, nao); +} + val zerop(val num) { if (num == zero) -- cgit v1.2.3