diff options
author | Damien George <damien.p.george@gmail.com> | 2014-03-22 20:34:43 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-03-22 20:34:43 +0000 |
commit | 26a4506da7725ce88f05361214b705b157630e2c (patch) | |
tree | ace60bf6e4f222566e51aeb163e3c03ecc68510f | |
parent | a6d53188b7db85af9dc93186e4f36b7009084ea6 (diff) | |
parent | 63c157e5347cbb8a80ef32c6365b537bba49485f (diff) | |
download | micropython-26a4506da7725ce88f05361214b705b157630e2c.tar.gz micropython-26a4506da7725ce88f05361214b705b157630e2c.zip |
Merge pull request #360 from rjdowdall/master
Fixed some math functions and added more exceptions.
-rw-r--r-- | py/intdivmod.c | 24 | ||||
-rw-r--r-- | py/intdivmod.h | 4 |
2 files changed, 28 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; + } +} diff --git a/py/intdivmod.h b/py/intdivmod.h new file mode 100644 index 0000000000..7716bd21e9 --- /dev/null +++ b/py/intdivmod.h @@ -0,0 +1,4 @@ +// Functions for integer modulo and floor division + +machine_int_t python_modulo(machine_int_t dividend, machine_int_t divisor); +machine_int_t python_floor_divide(machine_int_t num, machine_int_t denom); |