BigInt
t
type t = bigint
Type representing a bigint.
asIntN
let asIntN: (~width: int, bigint) => bigint
asIntN(~width, bigint)
returns a bigint value truncated to the given number of bits as a signed integer.
See BigInt.asIntN
on MDN.
Examples
RESCRIPTBigInt.asIntN(~width=4, 25n) == -7n
BigInt.asIntN(~width=4, 3n) == 3n
asUintN
let asUintN: (~width: int, bigint) => bigint
asUintN(~width, bigint)
returns a bigint value truncated to the given number of bits as an unsigned integer.
See BigInt.asUintN
on MDN.
Examples
RESCRIPTBigInt.asUintN(~width=4, 25n) == 9n
BigInt.asUintN(~width=4, 3n) == 3n
fromStringOrThrow
let fromStringOrThrow: string => bigint
Parses the given string
into a bigint
using JavaScript semantics. Return the
number as a bigint
if successfully parsed. Throws a syntax exception otherwise.
Examples
RESCRIPTBigInt.fromStringOrThrow("123") == 123n
BigInt.fromStringOrThrow("") == 0n
BigInt.fromStringOrThrow("0x11") == 17n
BigInt.fromStringOrThrow("0b11") == 3n
BigInt.fromStringOrThrow("0o11") == 9n
/* catch exception */
switch BigInt.fromStringOrThrow("a") {
| exception JsExn(_error) => assert(true)
| _bigInt => assert(false)
}
fromString
let fromString: string => option<bigint>
Parses the given string
into a bigint
using JavaScript semantics. Returns
Some(bigint)
if the string can be parsed, None
otherwise.
Examples
RESCRIPTBigInt.fromString("123") == Some(123n)
BigInt.fromString("") == Some(0n)
BigInt.fromString("0x11") == Some(17n)
BigInt.fromString("0b11") == Some(3n)
BigInt.fromString("0o11") == Some(9n)
BigInt.fromString("invalid") == None
fromStringExn
Deprecated
let fromStringExn: string => bigint
fromInt
let fromInt: int => bigint
fromInt(int)
converts an int
to a bigint
.
Examples
RESCRIPTBigInt.fromInt(123) == 123n
BigInt.fromInt(0) == 0n
BigInt.fromInt(-456) == -456n
fromFloatOrThrow
let fromFloatOrThrow: float => bigint
Converts a float
to a bigint
using JavaScript semantics.
Throws an exception if the float is not an integer or is infinite/NaN.
Examples
RESCRIPTBigInt.fromFloatOrThrow(123.0) == 123n
BigInt.fromFloatOrThrow(0.0) == 0n
BigInt.fromFloatOrThrow(-456.0) == -456n
/* This will throw an exception */
switch BigInt.fromFloatOrThrow(123.5) {
| exception JsExn(_error) => assert(true)
| _bigInt => assert(false)
}
fromFloat
let fromFloat: float => option<bigint>
fromFloat(float)
converts a float
to a bigint
using JavaScript semantics.
Returns Some(bigint)
if the float is a valid bigint
, None
otherwise.
Examples
RESCRIPTBigInt.fromFloat(123.0) == Some(123n)
BigInt.fromFloat(0.0) == Some(0n)
BigInt.fromFloat(-456.0) == Some(-456n)
BigInt.fromFloat(123.5) == None
toString
let toString: (bigint, ~radix: int=?) => string
Formats a bigint
as a string. Return a string
representing the given value.
See toString
on MDN.
Examples
RESCRIPTBigInt.toString(123n) == "123"
toStringWithRadix
Deprecated
let toStringWithRadix: (bigint, ~radix: int) => string
toLocaleString
let toLocaleString: bigint => string
Returns a string with a language-sensitive representation of this BigInt value.
Examples
RESCRIPTBigInt.toString(123n) == "123"
toFloat
let toFloat: bigint => float
toFloat(bigint)
converts a bigint
to a float
.
Examples
RESCRIPTBigInt.toFloat(123n) == 123.0
BigInt.toFloat(0n) == 0.0
BigInt.toFloat(-456n) == -456.0
toInt
let toInt: bigint => int
toInt(bigint)
converts a bigint
to an int
.
Examples
RESCRIPTBigInt.toInt(123n) == 123
BigInt.toInt(0n) == 0
BigInt.toInt(-456n) == -456
add
let add: (bigint, bigint) => bigint
add(a, b)
calculates the sum of two bigints.
Examples
RESCRIPTBigInt.add(5n, 3n) == 8n
BigInt.add(-2n, 7n) == 5n
sub
let sub: (bigint, bigint) => bigint
sub(a, b)
calculates the difference of two bigints.
Examples
RESCRIPTBigInt.sub(8n, 3n) == 5n
BigInt.sub(2n, 7n) == -5n
mul
let mul: (bigint, bigint) => bigint
mul(a, b)
calculates the product of two bigints.
Examples
RESCRIPTBigInt.mul(5n, 3n) == 15n
BigInt.mul(-2n, 7n) == -14n
div
let div: (bigint, bigint) => bigint
div(a, b)
calculates the quotient of two bigints.
Examples
RESCRIPTBigInt.div(15n, 3n) == 5n
BigInt.div(14n, 3n) == 4n
mod
let mod: (bigint, bigint) => bigint
mod(a, b)
calculates the remainder of dividing two bigints.
Examples
RESCRIPTBigInt.mod(15n, 4n) == 3n
BigInt.mod(14n, 3n) == 2n
bitwiseAnd
let bitwiseAnd: (bigint, bigint) => bigint
bitwiseAnd(a, b)
calculates the bitwise AND of two bigints.
Examples
RESCRIPTBigInt.bitwiseAnd(7n, 4n) == 4n
BigInt.bitwiseAnd(15n, 8n) == 8n
bitwiseOr
let bitwiseOr: (bigint, bigint) => bigint
bitwiseOr(a, b)
calculates the bitwise OR of two bigints.
Examples
RESCRIPTBigInt.bitwiseOr(7n, 4n) == 7n
BigInt.bitwiseOr(8n, 4n) == 12n
bitwiseXor
let bitwiseXor: (bigint, bigint) => bigint
bitwiseXor(a, b)
calculates the bitwise XOR of two bigints.
Examples
RESCRIPTBigInt.bitwiseXor(7n, 4n) == 3n
BigInt.bitwiseXor(15n, 8n) == 7n
bitwiseNot
let bitwiseNot: bigint => bigint
bitwiseNot(bigint)
calculates the bitwise NOT of a bigint.
Examples
RESCRIPTBigInt.bitwiseNot(2n) == -3n
BigInt.bitwiseNot(-1n) == 0n
shiftLeft
let shiftLeft: (bigint, bigint) => bigint
shiftLeft(bigint, amount)
calculates the shifted value of a bigint by amount
bits to the left.
Examples
RESCRIPTBigInt.shiftLeft(4n, 1n) == 8n
BigInt.shiftLeft(1n, 3n) == 8n
shiftRight
let shiftRight: (bigint, bigint) => bigint
shiftRight(bigint, amount)
calculates the shifted value of a bigint by amount
bits to the right.
Examples
RESCRIPTBigInt.shiftRight(8n, 1n) == 4n
BigInt.shiftRight(16n, 2n) == 4n
ignore
let ignore: bigint => unit
ignore(bigint)
ignores the provided bigint and returns unit.
This helper is useful when you want to discard a value (for example, the result of an operation with side effects) without having to store or process it further.
land
Deprecated
let land: (bigint, bigint) => bigint
land(a, b)
calculates the bitwise AND of two bigints.
Deprecated: Use &&&
operator or bitwiseAnd
instead.
Examples
RESCRIPTBigInt.land(7n, 4n) == 4n
lor
Deprecated
let lor: (bigint, bigint) => bigint
lor(a, b)
calculates the bitwise OR of two bigints.
Deprecated: Use |||
operator or bitwiseOr
instead.
Examples
RESCRIPTBigInt.lor(7n, 4n) == 7n
lxor
Deprecated
let lxor: (bigint, bigint) => bigint
lxor(a, b)
calculates the bitwise XOR of two bigints.
Deprecated: Use ^^^
operator or bitwiseXor
instead.
Examples
RESCRIPTBigInt.lxor(7n, 4n) == 3n
lnot
Deprecated
let lnot: bigint => bigint
lnot(bigint)
calculates the bitwise NOT of a bigint.
Deprecated: Use ~~~
operator or bitwiseNot
instead.
Examples
RESCRIPTBigInt.lnot(2n) == -3n
lsl
Deprecated
let lsl: (bigint, bigint) => bigint
lsl(bigint, amount)
calculates the shifted value of a bigint by amount
bits to the left.
Deprecated: Use <<
operator or shiftLeft
instead.
Examples
RESCRIPTBigInt.lsl(4n, 1n) == 8n
asr
Deprecated
let asr: (bigint, bigint) => bigint