summaryrefslogtreecommitdiffstats
path: root/arith.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2012-03-21 16:47:46 -0700
committerKaz Kylheku <kaz@kylheku.com>2012-03-21 16:47:46 -0700
commit3f7c28ed9255ce0332b2e9214ee771c8a1a8dd1c (patch)
treea70f7ae26d8314d94e4dddc72e0947bf4e6003e7 /arith.c
parent551a986c12660fa5a4b36fe22262e7d5255c9994 (diff)
downloadtxr-3f7c28ed9255ce0332b2e9214ee771c8a1a8dd1c.tar.gz
txr-3f7c28ed9255ce0332b2e9214ee771c8a1a8dd1c.tar.bz2
txr-3f7c28ed9255ce0332b2e9214ee771c8a1a8dd1c.zip
* 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.
Diffstat (limited to 'arith.c')
-rw-r--r--arith.c38
1 files changed, 38 insertions, 0 deletions
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)