Skip to content

Instantly share code, notes, and snippets.

@ydm
Created January 7, 2025 02:27
Show Gist options
  • Save ydm/7ed460da6adcdd1828e1e11171483477 to your computer and use it in GitHub Desktop.
Save ydm/7ed460da6adcdd1828e1e11171483477 to your computer and use it in GitHub Desktop.
divide :: biging -> bigint -> number
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