summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2017-08-07 20:14:17 -0700
committerKaz Kylheku <kaz@kylheku.com>2017-08-07 20:14:17 -0700
commit391abdde2d48aac8fc18978a1f3a26b9f0ae6bc8 (patch)
tree79506c783370195c69ae42282af76fce0cbbbb62
parent78ab514b4f3b481ae006821520c6344768a529b3 (diff)
downloadtxr-391abdde2d48aac8fc18978a1f3a26b9f0ae6bc8.tar.gz
txr-391abdde2d48aac8fc18978a1f3a26b9f0ae6bc8.tar.bz2
txr-391abdde2d48aac8fc18978a1f3a26b9f0ae6bc8.zip
New divides function.
* arith.c (divides): New function. (arith_init): Intrinsic registered. * arith.h (divides): Declared. * txr.1: Documented.
-rw-r--r--arith.c23
-rw-r--r--arith.h1
-rw-r--r--txr.129
3 files changed, 53 insertions, 0 deletions
diff --git a/arith.c b/arith.c
index 80405e85..7700ea07 100644
--- a/arith.c
+++ b/arith.c
@@ -2021,6 +2021,28 @@ val lcm(val anum, val bnum)
}
}
+val divides(val d, val n)
+{
+ if (n == zero) {
+ if (!integerp(d))
+ uw_throwf(error_s, lit("divides: ~s isn't an integer"),
+ d, nao);
+ return tnil(!zerop(d));
+ }
+
+ if (d == one) {
+ if (!integerp(n))
+ uw_throwf(error_s, lit("divides: ~s isn't an integer"),
+ n, nao);
+ return t;
+ }
+
+ if (minusp(d))
+ d = neg(d);
+
+ return eql(gcd(d, n), d);
+}
+
val floorf(val num)
{
switch (type(num)) {
@@ -2929,6 +2951,7 @@ void arith_init(void)
reg_varl(intern(lit("*e*"), user_package), flo(M_E));
reg_varl(intern(lit("%e%"), user_package), flo(M_E));
+ reg_fun(intern(lit("divides"), user_package), func_n2(divides));
reg_fun(intern(lit("bits"), system_package), func_n1(bits));
reg_fun(intern(lit("digpow"), user_package), func_n2o(digpow, 1));
reg_fun(intern(lit("digits"), user_package), func_n2o(digits, 1));
diff --git a/arith.h b/arith.h
index 29e65706..235d4ba1 100644
--- a/arith.h
+++ b/arith.h
@@ -39,6 +39,7 @@ val unum(ucnum u);
val cum_norm_dist(val x);
val n_choose_k(val n, val k);
val n_perm_k(val n, val k);
+val divides(val d, val n);
val tofloat(val obj);
val toint(val obj, val base);
val tofloatz(val obj);
diff --git a/txr.1 b/txr.1
index cec90274..e005a34b 100644
--- a/txr.1
+++ b/txr.1
@@ -33713,6 +33713,35 @@ If
.code lcm
has any argument which is zero, it yields zero.
+.coNP Function @ divides
+.synb
+.mets (divides < d << n )
+.syne
+.desc
+The
+.code divides
+function tests whether integer
+.meta d
+divides integer
+.metn n .
+If this is true,
+.code t
+is returned, otherwise
+.codn nil .
+
+The integers 1 and -1 divide every other integer and themselves.
+By established convention, every integer, except zero, divides zero.
+
+For other values,
+.meta d
+divides
+.meta n
+if division of
+.meta n
+by
+.meta d
+leaves no remainder.
+
.coNP Function @ abs
.synb
.mets (abs << number )