summaryrefslogtreecommitdiff
path: root/src/scheme/ao_scheme_builtin.c
diff options
context:
space:
mode:
authorKeith Packard <keithp@keithp.com>2017-12-24 14:28:29 -0800
committerKeith Packard <keithp@keithp.com>2017-12-24 14:31:31 -0800
commit7b5892f75a75363a656ede8befb419245aa218b5 (patch)
treebfc80cc2c541efc4524eaad82440386bc63fdbdb /src/scheme/ao_scheme_builtin.c
parentd95486be96fe989f6984b3452c5c5d92897c5606 (diff)
altos/scheme: Add separate floor-quotient builtin
Does what 'quotient' did before, now quotient rounds towards zero while floor-quotient rounds down. Signed-off-by: Keith Packard <keithp@keithp.com>
Diffstat (limited to 'src/scheme/ao_scheme_builtin.c')
-rw-r--r--src/scheme/ao_scheme_builtin.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/scheme/ao_scheme_builtin.c b/src/scheme/ao_scheme_builtin.c
index 81fd9010..e2532c98 100644
--- a/src/scheme/ao_scheme_builtin.c
+++ b/src/scheme/ao_scheme_builtin.c
@@ -393,6 +393,11 @@ ao_scheme_math(struct ao_scheme_cons *orig_cons, enum ao_scheme_builtin_id op)
case builtin_quotient:
if (c == 0)
return ao_scheme_error(AO_SCHEME_DIVIDE_BY_ZERO, "quotient by zero");
+ r = r / c;
+ break;
+ case builtin_floor_quotient:
+ if (c == 0)
+ return ao_scheme_error(AO_SCHEME_DIVIDE_BY_ZERO, "floor-quotient by zero");
if (r % c != 0 && (c < 0) != (r < 0))
r = r / c - 1;
else
@@ -436,6 +441,7 @@ ao_scheme_math(struct ao_scheme_cons *orig_cons, enum ao_scheme_builtin_id op)
r /= c;
break;
case builtin_quotient:
+ case builtin_floor_quotient:
case builtin_remainder:
case builtin_modulo:
return ao_scheme_error(AO_SCHEME_INVALID, "non-integer value in integer divide");
@@ -492,6 +498,12 @@ ao_scheme_do_quotient(struct ao_scheme_cons *cons)
}
ao_poly
+ao_scheme_do_floor_quotient(struct ao_scheme_cons *cons)
+{
+ return ao_scheme_math(cons, builtin_floor_quotient);
+}
+
+ao_poly
ao_scheme_do_modulo(struct ao_scheme_cons *cons)
{
return ao_scheme_math(cons, builtin_modulo);