Created
September 18, 2024 06:22
-
-
Save No-Eul/7a34cdf8312553dadb8ca228c6b89ab7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 floorMod(a, b) { | |
return a - b * Math.floor(a / b); | |
} | |
function toRadix(decimal, base) { | |
let array = []; | |
do array.unshift(floorMod(decimal, base)); | |
while ((decimal = Math.floor(decimal / base)) !== 0); | |
return array; | |
} | |
function toDecimal(radix, base) { | |
return radix.reverse().reduce((acc, cur, i, arr) => acc + cur * base ** i); | |
} | |
console.log(toRadix(-15, -2)) | |
console.log(toDecimal([ 4, 3, 7 ], -10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment