From 391abdde2d48aac8fc18978a1f3a26b9f0ae6bc8 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 7 Aug 2017 20:14:17 -0700 Subject: New divides function. * arith.c (divides): New function. (arith_init): Intrinsic registered. * arith.h (divides): Declared. * txr.1: Documented. --- arith.c | 23 +++++++++++++++++++++++ arith.h | 1 + txr.1 | 29 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) 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 ) -- cgit v1.2.3