Merge pull request #360 from rjdowdall/master

Fixed some math functions and added more exceptions.
This commit is contained in:
Damien George 2014-03-22 20:34:43 +00:00
commit 26a4506da7
2 changed files with 28 additions and 0 deletions

24
py/intdivmod.c Normal file
View File

@ -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;
}
}

4
py/intdivmod.h Normal file
View File

@ -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);