Created
January 7, 2025 02:27
-
-
Save ydm/7ed460da6adcdd1828e1e11171483477 to your computer and use it in GitHub Desktop.
divide :: biging -> bigint -> number
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 divide(a: bigint, b: bigint): number { | |
const PRECISION: bigint = 18n; | |
const EXP: bigint = 10n**PRECISION; | |
if (b === 0n) { | |
return 0; | |
} | |
const integer: bigint = a/b; | |
const remainder: bigint = a%b; | |
const fractional: bigint = (remainder * EXP) / b; | |
const result: bigint = integer * EXP + fractional; | |
return Number(result) / Number(EXP); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment