summaryrefslogtreecommitdiffstats
path: root/arith.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2011-12-13 19:37:19 -0800
committerKaz Kylheku <kaz@kylheku.com>2011-12-13 19:37:19 -0800
commit64b06932ed7d8dd8c904e66a70a53ae4c8ec4448 (patch)
tree643059d8904fda99b240ae6756100943a7a1c3e5 /arith.c
parentef47dfe4fcb7c1be369ae83221386b9da6474a1e (diff)
downloadtxr-64b06932ed7d8dd8c904e66a70a53ae4c8ec4448.tar.gz
txr-64b06932ed7d8dd8c904e66a70a53ae4c8ec4448.tar.bz2
txr-64b06932ed7d8dd8c904e66a70a53ae4c8ec4448.zip
* arith.c (evenp, oddp): New functions.
* eval.c (eval_init): New functions registered as intrinsics. * lib.h (evenp, oddp): Declared. * txr.1: Documentation stub updated.
Diffstat (limited to 'arith.c')
-rw-r--r--arith.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/arith.c b/arith.c
index 1ccab1f6..bde39aad 100644
--- a/arith.c
+++ b/arith.c
@@ -711,6 +711,36 @@ val zerop(val num)
return nil;
}
+val evenp(val num)
+{
+ switch (tag(num)) {
+ case TAG_NUM:
+ return (c_num(num) % 2 == 0) ? t : nil;
+ case TAG_PTR:
+ if (num->t.type == BGNUM)
+ return mp_iseven(mp(num)) ? t : nil;
+ /* fallthrough */
+ default:
+ uw_throwf(error_s, lit("evenp: ~s is not a number"), num, nao);
+ return nil;
+ }
+}
+
+val oddp(val num)
+{
+ switch (tag(num)) {
+ case TAG_NUM:
+ return (c_num(num) % 2 != 0) ? t : nil;
+ case TAG_PTR:
+ if (num->t.type == BGNUM)
+ return mp_isodd(mp(num)) ? t : nil;
+ /* fallthrough */
+ default:
+ uw_throwf(error_s, lit("oddp: ~s is not a number"), num, nao);
+ return nil;
+ }
+}
+
val gt(val anum, val bnum)
{
int tag_a = tag(anum);