forked from cykura/sdk-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqrt.ts
31 lines (26 loc) · 803 Bytes
/
sqrt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import JSBI from 'jsbi'
import invariant from 'tiny-invariant'
export const MAX_SAFE_INTEGER = JSBI.BigInt(Number.MAX_SAFE_INTEGER)
const ZERO = JSBI.BigInt(0)
const ONE = JSBI.BigInt(1)
const TWO = JSBI.BigInt(2)
/**
* Computes floor(sqrt(value))
* @param value the value for which to compute the square root, rounded down
*/
export function sqrt(value: JSBI): JSBI {
invariant(JSBI.greaterThanOrEqual(value, ZERO), 'NEGATIVE')
// rely on built in sqrt if possible
if (JSBI.lessThan(value, MAX_SAFE_INTEGER)) {
return JSBI.BigInt(Math.floor(Math.sqrt(JSBI.toNumber(value))))
}
let z: JSBI
let x: JSBI
z = value
x = JSBI.add(JSBI.divide(value, TWO), ONE)
while (JSBI.lessThan(x, z)) {
z = x
x = JSBI.divide(JSBI.add(JSBI.divide(value, x), x), TWO)
}
return z
}