forked from Uniswap/sdk-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c33cc04
commit 5a741a3
Showing
3 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import sortedInsert from './sortedInsert' | ||
import validateAndParseAddress from './validateAndParseAddress' | ||
import sqrt from './sqrt' | ||
|
||
export { sortedInsert, validateAndParseAddress } | ||
export { sortedInsert, validateAndParseAddress, sqrt } |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import JSBI from 'jsbi' | ||
import { MaxUint256 } from '../constants' | ||
import sqrt from './sqrt' | ||
|
||
describe('#sqrt', () => { | ||
it('correct for 0-1000', () => { | ||
for (let i = 0; i < 1000; i++) { | ||
expect(sqrt(JSBI.BigInt(i))).toEqual(JSBI.BigInt(Math.floor(Math.sqrt(i)))) | ||
} | ||
}) | ||
|
||
it('correct for all even powers of 2', async () => { | ||
for (let i = 0; i < 1000; i++) { | ||
const root = JSBI.exponentiate(JSBI.BigInt(2), JSBI.BigInt(i)) | ||
const rootSquared = JSBI.multiply(root, root) | ||
|
||
expect(sqrt(rootSquared)).toEqual(root) | ||
} | ||
}) | ||
|
||
it('correct for MaxUint256', () => { | ||
expect(sqrt(MaxUint256)).toEqual(JSBI.BigInt('340282366920938463463374607431768211455')) | ||
}) | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 default 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 | ||
} |