Arbitrary precision integers.
Example:
import std/jsbigints block: let big1: JsBigInt = big"2147483647" let big2: JsBigInt = big"666" doAssert JsBigInt isnot int doAssert big1 != big2 doAssert big1 > big2 doAssert big1 >= big2 doAssert big2 < big1 doAssert big2 <= big1 doAssert not(big1 == big2) let z = JsBigInt.default doAssert $z == "0n" block: var a: seq[JsBigInt] a.setLen 2 doAssert a == @[big"0", big"0"] doAssert a[^1] == big"0" var b: JsBigInt doAssert b == big"0" doAssert b == JsBigInt.default
Procs
func `'big`(num: cstring): JsBigInt {.importjs: "BigInt(#)", ...raises: [], tags: [], forbids: [].}
-
Constructor for JsBigInt.
Example:
doAssert -1'big == 1'big - 2'big # supports decimal, binary, octal, hex: doAssert -12'big == big"-12" doAssert 12'big == 12.big doAssert 0b101'big == 0b101.big doAssert 0o701'big == 0o701.big doAssert 0xdeadbeaf'big == 0xdeadbeaf.big doAssert 0xffffffffffffffff'big == (1'big shl 64'big) - 1'big doAssert not compiles(static(12'big))
Source Edit func `**`(x, y: JsBigInt): JsBigInt {.importjs: "((#) $1 #)", ...raises: [], tags: [], forbids: [].}
-
Example:
doAssert big"2" ** big"64" == big"18446744073709551616" doAssert big"-2" ** big"3" == big"-8" doAssert -big"2" ** big"2" == big"4" # parsed as: (-2n) ** 2n doAssert big"0" ** big"0" == big"1" # edge case var ok = false try: discard big"2" ** big"-1" # raises foreign `RangeError` except: ok = true doAssert ok
Source Edit func big(integer: cstring): JsBigInt {.importjs: "BigInt(#)", ...raises: [], tags: [], forbids: [].}
- Alias for 'big Source Edit
func big(integer: SomeInteger): JsBigInt {.importjs: "BigInt(#)", ...raises: [], tags: [], forbids: [].}
-
Constructor for JsBigInt.
Example:
doAssert big(1234567890) == big"1234567890" doAssert 0b1111100111.big == 0o1747.big and 0o1747.big == 999.big
Source Edit func `div`(x, y: JsBigInt): JsBigInt {.importjs: "(# / #)", ...raises: [], tags: [], forbids: [].}
-
Same as div but for JsBigInt(uses JavaScript BigInt() / BigInt()).
Example:
doAssert big"13" div big"3" == big"4" doAssert big"-13" div big"3" == big"-4" doAssert big"13" div big"-3" == big"-4" doAssert big"-13" div big"-3" == big"4"
Source Edit func `mod`(x, y: JsBigInt): JsBigInt {.importjs: "(# % #)", ...raises: [], tags: [], forbids: [].}
-
Same as mod but for JsBigInt (uses JavaScript BigInt() % BigInt()).
Example:
doAssert big"13" mod big"3" == big"1" doAssert big"-13" mod big"3" == big"-1" doAssert big"13" mod big"-3" == big"1" doAssert big"-13" mod big"-3" == big"-1"
Source Edit func toCstring(this: JsBigInt): cstring {.importjs: "#.toString()", ...raises: [], tags: [], forbids: [].}
- Converts from JsBigInt to cstring representation. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/toString Source Edit
func toCstring(this: JsBigInt; radix: 2 .. 36): cstring {. importjs: "#.toString(#)", ...raises: [], tags: [], forbids: [].}
-
Converts from JsBigInt to cstring representation.
- radix Base to use for representing numeric values.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/toString
Example:
doAssert big"2147483647".toCstring(2) == "1111111111111111111111111111111".cstring
Source Edit func wrapToInt(this: JsBigInt; bits: Natural): JsBigInt {. importjs: "(() => { const i = #, b = #; return BigInt.asIntN(b, i) })()", ...raises: [], tags: [], forbids: [].}
-
Wraps this to a signed JsBigInt of bits bits in -2 ^ (bits - 1) .. 2 ^ (bits - 1) - 1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/asIntN
Example:
doAssert (big("3") + big("2") ** big("66")).wrapToInt(13) == big("3")
Source Edit func wrapToUint(this: JsBigInt; bits: Natural): JsBigInt {. importjs: "(() => { const i = #, b = #; return BigInt.asUintN(b, i) })()", ...raises: [], tags: [], forbids: [].}
-
Wraps this to an unsigned JsBigInt of bits bits in 0 .. 2 ^ bits - 1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/asUintN
Example:
doAssert (big("3") + big("2") ** big("66")).wrapToUint(66) == big("3")
Source Edit