2022-08-25 00:59:30 +00:00
|
|
|
import Returns from '../core/Returns.mjs'
|
2022-07-30 11:59:04 +00:00
|
|
|
export * from './Types/bigint.mjs'
|
|
|
|
|
|
|
|
/* Returns the closest integer approximation to n/d */
|
|
|
|
export const roundquotient = {
|
2022-08-25 00:59:30 +00:00
|
|
|
'bigint,bigint': ({'sign(bigint)': sgn}) => Returns('bigint', (n, d) => {
|
2022-07-30 11:59:04 +00:00
|
|
|
const dSgn = sgn(d)
|
|
|
|
if (dSgn === 0n) return 0n
|
|
|
|
const candidate = n/d
|
|
|
|
const rem = n - d*candidate
|
|
|
|
const absd = d*dSgn
|
|
|
|
if (2n * rem > absd) return candidate + dSgn
|
|
|
|
if (-2n * rem >= absd) return candidate - dSgn
|
|
|
|
return candidate
|
2022-08-25 00:59:30 +00:00
|
|
|
})
|
2022-07-30 11:59:04 +00:00
|
|
|
}
|