(n: bigint): bigintReturns the integer square root of a non-negative bigint.
When to use
Use when you need to compute an integer square root for a bigint that has
already been validated as non-negative, and you want negative input to throw
instead of returning Option.none.
Details
For non-perfect squares, returns the largest bigint whose square is less
than or equal to the input.
Gotchas
Throws a RangeError if the input is negative.
Example (Calculating square roots unsafely)
import { BigInt } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigInt.sqrtUnsafe(4n), 2n)
assert.deepStrictEqual(BigInt.sqrtUnsafe(9n), 3n)
assert.deepStrictEqual(BigInt.sqrtUnsafe(16n), 4n)export const const sqrtUnsafe: (n: bigint) => bigintReturns the integer square root of a non-negative bigint.
When to use
Use when you need to compute an integer square root for a bigint that has
already been validated as non-negative, and you want negative input to throw
instead of returning Option.none.
Details
For non-perfect squares, returns the largest bigint whose square is less
than or equal to the input.
Gotchas
Throws a RangeError if the input is negative.
Example (Calculating square roots unsafely)
import { BigInt } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigInt.sqrtUnsafe(4n), 2n)
assert.deepStrictEqual(BigInt.sqrtUnsafe(9n), 3n)
assert.deepStrictEqual(BigInt.sqrtUnsafe(16n), 4n)
sqrtUnsafe = (n: bigintn: bigint): bigint => {
if (n: bigintn < const bigint0: bigintbigint0) {
throw new var RangeError: RangeErrorConstructor
new (message?: string, options?: ErrorOptions) => RangeError (+3 overloads)
RangeError("Cannot take the square root of a negative number")
}
if (n: bigintn < const bigint2: bigintbigint2) {
return n: bigintn
}
let let x: bigintx = n: bigintn / const bigint2: bigintbigint2
while (let x: bigintx * let x: bigintx > n: bigintn) {
let x: bigintx = ((n: bigintn / let x: bigintx) + let x: bigintx) / const bigint2: bigintbigint2
}
return let x: bigintx
}