Last active
May 25, 2020 00:45
-
-
Save westc/85320e5d7e7788cb114a57fc19394632 to your computer and use it in GitHub Desktop.
Definition for divMod() in JavaScript. Differs from python's version due to how modulo is defined by the language.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function divmod(a, b) { | |
var remainder = a % b; | |
var aIsInfinite = a === -Infinity || a === Infinity; | |
var bIsInfinite = b === -Infinity || b === Infinity; | |
var aIsNeg = a + 1 / a < 0; | |
var bIsNeg = b + 1 / b < 0; | |
return [ | |
(aIsInfinite !== bIsInfinite && a) ? aIsInfinite ? NaN : aIsNeg === bIsNeg ? 0 : -1 : Math.floor(a / b), | |
(!a && b < 0) ? -0 : remainder + (remainder !== 0 && aIsNeg !== bIsNeg ? b : 0) | |
]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function divMod(a, b) { | |
return [Math.trunc(a / b), a % b]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// HIDE \\ | |
console.load('local://python-style-divmod.js'); | |
// HIDE \\ | |
function isSameNumber(a, b) { | |
return a === 0 | |
? 1 / a === 1 / b | |
: (a === b || (Number.isNaN(a) && Number.isNaN(b))); | |
} | |
function pyStr(value) { | |
return value === Infinity | |
? "float('inf')" | |
: value === -Infinity | |
? "-float('inf')" | |
: Number.isNaN(value) | |
? "float('nan')" | |
: str(value).replace(/^-?\d+$/, '$&.0'); | |
} | |
function str(value) { | |
return value === 0 ? 1 / value < 0 ? '-0' : '0' : value + ''; | |
} | |
var tests = [ | |
[-Infinity, -Infinity, NaN, NaN], | |
[-Infinity, -7, NaN, NaN], | |
[-Infinity, -6.5, NaN, NaN], | |
[-Infinity, -3, NaN, NaN], | |
[-Infinity, 3, NaN, NaN], | |
[-Infinity, 6.5, NaN, NaN], | |
[-Infinity, 7, NaN, NaN], | |
[-Infinity, Infinity, NaN, NaN], | |
[-7, -Infinity, 0.0, -7.0], | |
[-7, -7, 1, 0], | |
[-7, -6.5, 1.0, -0.5], | |
[-7, -3, 2, -1], | |
[-7, 3, -3, 2], | |
[-7, 6.5, -2.0, 6.0], | |
[-7, 7, -1, 0], | |
[-7, Infinity, -1.0, Infinity], | |
[-4.5, -Infinity, 0.0, -4.5], | |
[-4.5, -7, 0.0, -4.5], | |
[-4.5, -6.5, 0.0, -4.5], | |
[-4.5, -3, 1.0, -1.5], | |
[-4.5, 3, -2.0, 1.5], | |
[-4.5, 6.5, -1.0, 2.0], | |
[-4.5, 7, -1.0, 2.5], | |
[-4.5, Infinity, -1.0, Infinity], | |
[-0.0, -Infinity, 0.0, -0.0], | |
[-0.0, -7, 0.0, -0.0], | |
[-0.0, -6.5, 0.0, -0.0], | |
[-0.0, -3, 0.0, -0.0], | |
[-0.0, 3, -0.0, 0.0], | |
[-0.0, 6.5, -0.0, 0.0], | |
[-0.0, 7, -0.0, 0.0], | |
[-0.0, Infinity, -0.0, 0.0], | |
[0.0, -Infinity, -0.0, -0.0], | |
[0.0, -7, -0.0, -0.0], | |
[0.0, -6.5, -0.0, -0.0], | |
[0.0, -3, -0.0, -0.0], | |
[0.0, 3, 0.0, 0.0], | |
[0.0, 6.5, 0.0, 0.0], | |
[0.0, 7, 0.0, 0.0], | |
[0.0, Infinity, 0.0, 0.0], | |
[4.5, -Infinity, -1.0, -Infinity], | |
[4.5, -7, -1.0, -2.5], | |
[4.5, -6.5, -1.0, -2.0], | |
[4.5, -3, -2.0, -1.5], | |
[4.5, 3, 1.0, 1.5], | |
[4.5, 6.5, 0.0, 4.5], | |
[4.5, 7, 0.0, 4.5], | |
[4.5, Infinity, 0.0, 4.5], | |
[7, -Infinity, -1.0, -Infinity], | |
[7, -7, -1, 0], | |
[7, -6.5, -2.0, -6.0], | |
[7, -3, -3, -2], | |
[7, 3, 2, 1], | |
[7, 6.5, 1.0, 0.5], | |
[7, 7, 1, 0], | |
[7, Infinity, 0.0, 7.0], | |
[Infinity, -Infinity, NaN, NaN], | |
[Infinity, -7, NaN, NaN], | |
[Infinity, -6.5, NaN, NaN], | |
[Infinity, -3, NaN, NaN], | |
[Infinity, 3, NaN, NaN], | |
[Infinity, 6.5, NaN, NaN], | |
[Infinity, 7, NaN, NaN], | |
[Infinity, Infinity, NaN, NaN] | |
]; | |
//\\ | |
tests.forEach(function(values, index) { | |
var a = values[0]; | |
var b = values[1]; | |
var e0 = values[2]; | |
var e1 = values[3]; | |
var results = divmod(a, b); | |
var r0 = results[0]; | |
var r1 = results[1]; | |
var expr = 'Python: divmod(' + pyStr(a) + ', ' + pyStr(b) + ') \u21E8 (' + pyStr(e0) + ', ' + pyStr(e1) + ')'; | |
var rExpr = '[' + str(r0) + ', ' + str(r1) + '] \u21E6 JS: divmod(' + str(a) + ', ' + str(b) + ')'; | |
var isRight = isSameNumber(e0, r0) && isSameNumber(e1, r1); | |
console[isRight ? 'log' : 'error']('(' + (index + 1) + ')\t' + expr + (isRight ? ' = ' : ' \u2260 ') + rExpr); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Testing the Python-style
divmod()
function:https://www.yourjs.com/console/?gist=85320e5d7e7788cb114a57fc19394632&file=~test-python-style-divmod.js&skipIntro=1