summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2014-07-20 09:07:20 -0700
committerKaz Kylheku <kaz@kylheku.com>2014-07-20 09:07:20 -0700
commit7da23010b2afbaae4dc4b8f01d1f8950e2c878be (patch)
tree8d11d7966345d4c4c93ea2b201ef0b5d98728f35
parente4caf8527d35df820c602b4e75a810f43082e39e (diff)
downloadtxr-7da23010b2afbaae4dc4b8f01d1f8950e2c878be.tar.gz
txr-7da23010b2afbaae4dc4b8f01d1f8950e2c878be.tar.bz2
txr-7da23010b2afbaae4dc4b8f01d1f8950e2c878be.zip
* arith.c (divi): Support one-argument form.
Use "/" name in error reporting, not "divi". * eval.c (eval_init): Change registration of / so only one argument is required out of two. * txr.1: Document one-argument division.
-rw-r--r--ChangeLog10
-rw-r--r--arith.c17
-rw-r--r--eval.c2
-rw-r--r--txr.15
4 files changed, 26 insertions, 8 deletions
diff --git a/ChangeLog b/ChangeLog
index ff4b0b7d..ae8e9302 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
2014-07-20 Kaz Kylheku <kaz@kylheku.com>
+ * arith.c (divi): Support one-argument form.
+ Use "/" name in error reporting, not "divi".
+
+ * eval.c (eval_init): Change registration of / so only
+ one argument is required out of two.
+
+ * txr.1: Document one-argument division.
+
+2014-07-20 Kaz Kylheku <kaz@kylheku.com>
+
* genvim.txr: Fixed highlighting issues in numbers followed by newline.
* txr.vim: Regenerated.
diff --git a/arith.c b/arith.c
index 14d3d0e2..7051ddb8 100644
--- a/arith.c
+++ b/arith.c
@@ -946,13 +946,20 @@ static val to_float(val func, val num)
val divi(val anum, val bnum)
{
- double a = c_flo(to_float(lit("divi"), anum));
- double b = c_flo(to_float(lit("divi"), bnum));
+ if (missingp(bnum)) {
+ double b = c_flo(to_float(lit("/"), anum));
+ if (b == 0.0)
+ uw_throw(numeric_error_s, lit("/: division by zero"));
+ return flo(1.0 / b);
+ } else {
+ double a = c_flo(to_float(lit("/"), anum));
+ double b = c_flo(to_float(lit("/"), bnum));
- if (b == 0.0)
- uw_throw(numeric_error_s, lit("divi: division by zero"));
+ if (b == 0.0)
+ uw_throw(numeric_error_s, lit("/: division by zero"));
- return flo(a / b);
+ return flo(a / b);
+ }
}
val zerop(val num)
diff --git a/eval.c b/eval.c
index 6e596272..0cd9b73d 100644
--- a/eval.c
+++ b/eval.c
@@ -3594,7 +3594,7 @@ void eval_init(void)
reg_fun(intern(lit("abs"), user_package), func_n1(abso));
reg_fun(intern(lit("trunc"), user_package), func_n2(trunc));
reg_fun(intern(lit("mod"), user_package), func_n2(mod));
- reg_fun(intern(lit("/"), user_package), func_n2(divi));
+ reg_fun(intern(lit("/"), user_package), func_n2o(divi, 1));
reg_fun(intern(lit("expt"), user_package), func_n0v(exptv));
reg_fun(intern(lit("exptmod"), user_package), func_n3(exptmod));
reg_fun(intern(lit("isqrt"), user_package), func_n1(isqrt));
diff --git a/txr.1 b/txr.1
index b3f4b3b8..5ffacc85 100644
--- a/txr.1
+++ b/txr.1
@@ -10807,7 +10807,7 @@ A character may not be an operand of multiplication.
.TP
Syntax:
- (/ <dividend> <divisor>)
+ (/ [<dividend>] <divisor>)
(trunc <dividend> <divisor>)
(mod <dividend> <divisor>)
@@ -10816,7 +10816,8 @@ Description:
The arguments to these functions are numbers. Characters are not permitted.
The / function performs floating-point division. Each operands is first
-converted to floating-point type, if necessary.
+converted to floating-point type, if necessary. If <dividend> is omitted,
+then it is taken to be 1.0 and the function performs a reciprocal.
The trunc function performs a division of <dividend> by <divisor> whose result
is truncated to integer toward zero. If both operands are integers, then an