Source Edit

Arbitrary precision integers.

Example:

  1. import std/jsbigints
  2. block:
  3. let big1: JsBigInt = big"2147483647"
  4. let big2: JsBigInt = big"666"
  5. doAssert JsBigInt isnot int
  6. doAssert big1 != big2
  7. doAssert big1 > big2
  8. doAssert big1 >= big2
  9. doAssert big2 < big1
  10. doAssert big2 <= big1
  11. doAssert not(big1 == big2)
  12. let z = JsBigInt.default
  13. doAssert $z == "0n"
  14. block:
  15. var a: seq[JsBigInt]
  16. a.setLen 2
  17. doAssert a == @[big"0", big"0"]
  18. doAssert a[^1] == big"0"
  19. var b: JsBigInt
  20. doAssert b == big"0"
  21. doAssert b == JsBigInt.default

Types

  1. JsBigInt = distinct JsBigIntImpl

Arbitrary precision integer for JavaScript target. Source Edit

Procs

  1. func `$`(this: JsBigInt): string {....raises: [], tags: [], forbids: [].}

Returns a string representation of JsBigInt.

Example:

  1. doAssert $big"1024" == "1024n"

Source Edit

  1. func `'big`(num: cstring): JsBigInt {.importjs: "BigInt(#)", ...raises: [],
  2. tags: [], forbids: [].}

Constructor for JsBigInt.

Example:

  1. doAssert -1'big == 1'big - 2'big
  2. # supports decimal, binary, octal, hex:
  3. doAssert -12'big == big"-12"
  4. doAssert 12'big == 12.big
  5. doAssert 0b101'big == 0b101.big
  6. doAssert 0o701'big == 0o701.big
  7. doAssert 0xdeadbeaf'big == 0xdeadbeaf.big
  8. doAssert 0xffffffffffffffff'big == (1'big shl 64'big) - 1'big
  9. doAssert not compiles(static(12'big))

Source Edit

  1. func `*`(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)", ...raises: [], tags: [],
  2. forbids: [].}

Example:

  1. doAssert (big"42" * big"9") == big"378"

Source Edit

  1. func `**`(x, y: JsBigInt): JsBigInt {.importjs: "((#) $1 #)", ...raises: [],
  2. tags: [], forbids: [].}

Example:

  1. doAssert big"2" ** big"64" == big"18446744073709551616"
  2. doAssert big"-2" ** big"3" == big"-8"
  3. doAssert -big"2" ** big"2" == big"4" # parsed as: (-2n) ** 2n
  4. doAssert big"0" ** big"0" == big"1" # edge case
  5. var ok = false
  6. try: discard big"2" ** big"-1" # raises foreign `RangeError`
  7. except: ok = true
  8. doAssert ok

Source Edit

  1. func `*=`(x: var JsBigInt; y: JsBigInt) {.importjs: "([#][0][0] $1 #)",
  2. ...raises: [], tags: [], forbids: [].}

Example:

  1. var big1: JsBigInt = big"2"
  2. big1 *= big"4"
  3. doAssert big1 == big"8"

Source Edit

  1. proc `+`(_: JsBigInt): JsBigInt {.error: "See https://github.com/tc39/proposal-bigint/blob/master/ADVANCED.md#dont-break-asmjs".}

Do NOT use. https://github.com/tc39/proposal-bigint/blob/master/ADVANCED.md#dont-break-asmjs Source Edit

  1. func `+`(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)", ...raises: [], tags: [],
  2. forbids: [].}

Example:

  1. doAssert (big"9" + big"1") == big"10"

Source Edit

  1. func `+=`(x: var JsBigInt; y: JsBigInt) {.importjs: "([#][0][0] $1 #)",
  2. ...raises: [], tags: [], forbids: [].}

Example:

  1. var big1: JsBigInt = big"1"
  2. big1 += big"2"
  3. doAssert big1 == big"3"

Source Edit

  1. func `-`(this: JsBigInt): JsBigInt {.importjs: "($1#)", ...raises: [], tags: [],
  2. forbids: [].}

Example:

  1. doAssert -(big"10101010101") == big"-10101010101"

Source Edit

  1. func `-`(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)", ...raises: [], tags: [],
  2. forbids: [].}

Example:

  1. doAssert (big"9" - big"1") == big"8"

Source Edit

  1. func `-=`(x: var JsBigInt; y: JsBigInt) {.importjs: "([#][0][0] $1 #)",
  2. ...raises: [], tags: [], forbids: [].}

Example:

  1. var big1: JsBigInt = big"1"
  2. big1 -= big"2"
  3. doAssert big1 == big"-1"

Source Edit

  1. func `/=`(x: var JsBigInt; y: JsBigInt) {.importjs: "([#][0][0] $1 #)",
  2. ...raises: [], tags: [], forbids: [].}

Same as x = x div y.

Example:

  1. var big1: JsBigInt = big"11"
  2. big1 /= big"2"
  3. doAssert big1 == big"5"

Source Edit

  1. func `<`(x, y: JsBigInt): bool {.importjs: "(# $1 #)", ...raises: [], tags: [],
  2. forbids: [].}

Example:

  1. doAssert big"2" < big"9"

Source Edit

  1. func `<=`(x, y: JsBigInt): bool {.importjs: "(# $1 #)", ...raises: [], tags: [],
  2. forbids: [].}

Example:

  1. doAssert big"1" <= big"5"

Source Edit

  1. func `==`(x, y: JsBigInt): bool {.importjs: "(# == #)", ...raises: [], tags: [],
  2. forbids: [].}

Example:

  1. doAssert big"42" == big"42"

Source Edit

  1. func `and`(x, y: JsBigInt): JsBigInt {.importjs: "(# & #)", ...raises: [],
  2. tags: [], forbids: [].}

Example:

  1. doAssert (big"555" and big"2") == big"2"

Source Edit

  1. func big(integer: cstring): JsBigInt {.importjs: "BigInt(#)", ...raises: [],
  2. tags: [], forbids: [].}

Alias for ‘big Source Edit

  1. func big(integer: SomeInteger): JsBigInt {.importjs: "BigInt(#)", ...raises: [],
  2. tags: [], forbids: [].}

Constructor for JsBigInt.

Example:

  1. doAssert big(1234567890) == big"1234567890"
  2. doAssert 0b1111100111.big == 0o1747.big and 0o1747.big == 999.big

Source Edit

  1. func dec(this: var JsBigInt) {.importjs: "(--[#][0][0])", ...raises: [], tags: [],
  2. forbids: [].}

Example:

  1. var big1: JsBigInt = big"2"
  2. dec big1
  3. doAssert big1 == big"1"

Source Edit

  1. func dec(this: var JsBigInt; amount: JsBigInt) {.importjs: "([#][0][0] -= #)",
  2. ...raises: [], tags: [], forbids: [].}

Example:

  1. var big1: JsBigInt = big"1"
  2. dec big1, big"2"
  3. doAssert big1 == big"-1"

Source Edit

  1. func `div`(x, y: JsBigInt): JsBigInt {.importjs: "(# / #)", ...raises: [],
  2. tags: [], forbids: [].}

Same as div but for JsBigInt(uses JavaScript BigInt() / BigInt()).

Example:

  1. doAssert big"13" div big"3" == big"4"
  2. doAssert big"-13" div big"3" == big"-4"
  3. doAssert big"13" div big"-3" == big"-4"
  4. doAssert big"-13" div big"-3" == big"4"

Source Edit

  1. proc high(_: typedesc[JsBigInt]): JsBigInt {.
  2. error: "Arbitrary precision integers do not have a known high.".}

Do NOT use. Source Edit

  1. func inc(this: var JsBigInt) {.importjs: "(++[#][0][0])", ...raises: [], tags: [],
  2. forbids: [].}

Example:

  1. var big1: JsBigInt = big"1"
  2. inc big1
  3. doAssert big1 == big"2"

Source Edit

  1. func inc(this: var JsBigInt; amount: JsBigInt) {.importjs: "([#][0][0] += #)",
  2. ...raises: [], tags: [], forbids: [].}

Example:

  1. var big1: JsBigInt = big"1"
  2. inc big1, big"2"
  3. doAssert big1 == big"3"

Source Edit

  1. proc low(_: typedesc[JsBigInt]): JsBigInt {.
  2. error: "Arbitrary precision integers do not have a known low.".}

Do NOT use. Source Edit

  1. func `mod`(x, y: JsBigInt): JsBigInt {.importjs: "(# % #)", ...raises: [],
  2. tags: [], forbids: [].}

Same as mod but for JsBigInt (uses JavaScript BigInt() % BigInt()).

Example:

  1. doAssert big"13" mod big"3" == big"1"
  2. doAssert big"-13" mod big"3" == big"-1"
  3. doAssert big"13" mod big"-3" == big"1"
  4. doAssert big"-13" mod big"-3" == big"-1"

Source Edit

  1. func `or`(x, y: JsBigInt): JsBigInt {.importjs: "(# | #)", ...raises: [], tags: [],
  2. forbids: [].}

Example:

  1. doAssert (big"555" or big"2") == big"555"

Source Edit

  1. func `shl`(a, b: JsBigInt): JsBigInt {.importjs: "(# << #)", ...raises: [],
  2. tags: [], forbids: [].}

Example:

  1. doAssert (big"999" shl big"2") == big"3996"

Source Edit

  1. func `shr`(a, b: JsBigInt): JsBigInt {.importjs: "(# >> #)", ...raises: [],
  2. tags: [], forbids: [].}

Example:

  1. doAssert (big"999" shr big"2") == big"249"

Source Edit

  1. func toCstring(this: JsBigInt): cstring {.importjs: "#.toString()", ...raises: [],
  2. tags: [], forbids: [].}

Converts from JsBigInt to cstring representation. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/toString Source Edit

  1. func toCstring(this: JsBigInt; radix: 2 .. 36): cstring {.
  2. 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:

  1. doAssert big"2147483647".toCstring(2) == "1111111111111111111111111111111".cstring

Source Edit

  1. func toNumber(this: JsBigInt): int {.importjs: "Number(#)", ...raises: [],
  2. tags: [], forbids: [].}

Does not do any bounds check and may or may not return an inexact representation.

Example:

  1. doAssert toNumber(big"2147483647") == 2147483647.int

Source Edit

  1. func wrapToInt(this: JsBigInt; bits: Natural): JsBigInt {.
  2. importjs: "(() => { const i = #, b = #; return BigInt.asIntN(b, i) })()",
  3. ...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:

  1. doAssert (big("3") + big("2") ** big("66")).wrapToInt(13) == big("3")

Source Edit

  1. func wrapToUint(this: JsBigInt; bits: Natural): JsBigInt {.
  2. importjs: "(() => { const i = #, b = #; return BigInt.asUintN(b, i) })()",
  3. ...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:

  1. doAssert (big("3") + big("2") ** big("66")).wrapToUint(66) == big("3")

Source Edit

  1. func `xor`(x, y: JsBigInt): JsBigInt {.importjs: "(# ^ #)", ...raises: [],
  2. tags: [], forbids: [].}

Example:

  1. doAssert (big"555" xor big"2") == big"553"

Source Edit