Hyperlinkv0.8.0-beta.28

BigInt

BigInt.sqrtconsteffect/BigInt.ts:743
(n: bigint): Option.Option<bigint>

Computes the integer square root of a bigint safely.

When to use

Use to compute an integer square root while representing negative input as Option.none.

Details

For non-perfect squares, returns the largest bigint whose square is less than or equal to the input. Returns Option.none() when the input is negative.

Example (Calculating square roots safely)

import { BigInt } from "effect"

BigInt.sqrt(4n) // Option.some(2n)
BigInt.sqrt(9n) // Option.some(3n)
BigInt.sqrt(16n) // Option.some(4n)
BigInt.sqrt(-1n) // Option.none()
Source effect/BigInt.ts:7432 lines
export const sqrt = (n: bigint): Option.Option<bigint> =>
  isGreaterThanOrEqualTo(n, bigint0) ? Option.some(sqrtUnsafe(n)) : Option.none()