diff options
Diffstat (limited to 'index.js')
| -rw-r--r-- | index.js | 11 |
1 files changed, 8 insertions, 3 deletions
@@ -19,7 +19,7 @@ basic.validate_digits = function(s, b) { // Validate that a base is valid. basic.validate_base = function(b){ - return (b === 0 || b === 1 || b === -1 && (Math.abs(b) % 2) === 0) + return (b !== 0 && b !== 1 && b !== -1 && (Math.abs(b) % 2) === 0) } // Convert a string s representing a number in base b @@ -45,7 +45,7 @@ basic.toString = function (n, b) { var s = "" var dividend, residual while (n) { - remainder = mod(n, b) + remainder = basic.mod(n, b) n = Math.floor(n/b) if (remainder) { n += 1 @@ -55,5 +55,10 @@ basic.toString = function (n, b) { return s } -function mod(n,m){ return n-(m * Math.floor(n/m)) } +// Compute the residual of N mod M +// Handle negative N and M correctly +basic.mod = function (n,m){ + m = Math.abs(m) + return n-(m * Math.floor(n/m)) +} |
