summaryrefslogtreecommitdiffstatshomepage
path: root/py/intdivmod.c
diff options
context:
space:
mode:
authorRachel Dowdall <rjdowdall@gmail.com>2014-03-22 20:29:56 +0000
committerRachel Dowdall <rjdowdall@gmail.com>2014-03-22 20:29:56 +0000
commit2d15deebdcf7d6fb8f78907e0c6ca9cb9c2aa000 (patch)
tree4dba583967542dd95ebdf3f62865f9bb56db2350 /py/intdivmod.c
parentcde8631f15db9941986f8d04534e52462a76094b (diff)
downloadmicropython-2d15deebdcf7d6fb8f78907e0c6ca9cb9c2aa000.tar.gz
micropython-2d15deebdcf7d6fb8f78907e0c6ca9cb9c2aa000.zip
Fixed floor division on mp ints and small ints. Added a floordivide test case.
Diffstat (limited to 'py/intdivmod.c')
-rw-r--r--py/intdivmod.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/py/intdivmod.c b/py/intdivmod.c
new file mode 100644
index 0000000000..4cb363b511
--- /dev/null
+++ b/py/intdivmod.c
@@ -0,0 +1,24 @@
+#include "mpconfig.h"
+
+machine_int_t python_modulo(machine_int_t dividend, machine_int_t divisor) {
+ machine_int_t lsign = (dividend >= 0) ? 1 :-1;
+ machine_int_t rsign = (divisor >= 0) ? 1 :-1;
+ dividend %= divisor;
+ if (lsign != rsign) {
+ dividend += divisor;
+ }
+ return dividend;
+}
+
+
+machine_int_t python_floor_divide(machine_int_t num, machine_int_t denom) {
+ machine_int_t lsign = num > 0 ? 1 : -1;
+ machine_int_t rsign = denom > 0 ? 1 : -1;
+ if (lsign == -1) {num *= -1;}
+ if (rsign == -1) {denom *= -1;}
+ if (lsign != rsign){
+ return - ( num + denom - 1) / denom;
+ } else {
+ return num / denom;
+ }
+}