std/jsbigints

Source   Edit  

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

Types

JsBigInt = distinct JsBigIntImpl
Arbitrary precision integer for JavaScript target. Source   Edit  

Procs

func `$`(this: JsBigInt): string {....raises: [], tags: [], forbids: [].}
Returns a string representation of JsBigInt.

Example:

doAssert $big"1024" == "1024n"
Source   Edit  
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"42" * big"9") == big"378"
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 `*=`(x: var JsBigInt; y: JsBigInt) {.importjs: "([#][0][0] $1 #)",
    ...raises: [], tags: [], forbids: [].}

Example:

var big1: JsBigInt = big"2"
big1 *= big"4"
doAssert big1 == big"8"
Source   Edit  
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  
func `+`(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)", ...raises: [], tags: [],
                                     forbids: [].}

Example:

doAssert (big"9" + big"1") == big"10"
Source   Edit  
func `+=`(x: var JsBigInt; y: JsBigInt) {.importjs: "([#][0][0] $1 #)",
    ...raises: [], tags: [], forbids: [].}

Example:

var big1: JsBigInt = big"1"
big1 += big"2"
doAssert big1 == big"3"
Source   Edit  
func `-`(this: JsBigInt): JsBigInt {.importjs: "($1#)", ...raises: [], tags: [],
                                     forbids: [].}

Example:

doAssert -(big"10101010101") == big"-10101010101"
Source   Edit  
func `-`(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)", ...raises: [], tags: [],
                                     forbids: [].}

Example:

doAssert (big"9" - big"1") == big"8"
Source   Edit  
func `-=`(x: var JsBigInt; y: JsBigInt) {.importjs: "([#][0][0] $1 #)",
    ...raises: [], tags: [], forbids: [].}

Example:

var big1: JsBigInt = big"1"
big1 -= big"2"
doAssert big1 == big"-1"
Source   Edit  
func `/=`(x: var JsBigInt; y: JsBigInt) {.importjs: "([#][0][0] $1 #)",
    ...raises: [], tags: [], forbids: [].}
Same as x = x div y.

Example:

var big1: JsBigInt = big"11"
big1 /= big"2"
doAssert big1 == big"5"
Source   Edit  
func `<`(x, y: JsBigInt): bool {.importjs: "(# $1 #)", ...raises: [], tags: [],
                                 forbids: [].}

Example:

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

Example:

doAssert big"1" <= big"5"
Source   Edit  
func `==`(x, y: JsBigInt): bool {.importjs: "(# == #)", ...raises: [], tags: [],
                                  forbids: [].}

Example:

doAssert big"42" == big"42"
Source   Edit  
func `and`(x, y: JsBigInt): JsBigInt {.importjs: "(# & #)", ...raises: [],
                                       tags: [], forbids: [].}

Example:

doAssert (big"555" and big"2") == big"2"
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 dec(this: var JsBigInt) {.importjs: "(--[#][0][0])", ...raises: [], tags: [],
                               forbids: [].}

Example:

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

Example:

var big1: JsBigInt = big"1"
dec big1, big"2"
doAssert big1 == big"-1"
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  
proc high(_: typedesc[JsBigInt]): JsBigInt {.
    error: "Arbitrary precision integers do not have a known high.".}
Do NOT use. Source   Edit  
func inc(this: var JsBigInt) {.importjs: "(++[#][0][0])", ...raises: [], tags: [],
                               forbids: [].}

Example:

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

Example:

var big1: JsBigInt = big"1"
inc big1, big"2"
doAssert big1 == big"3"
Source   Edit  
proc low(_: typedesc[JsBigInt]): JsBigInt {.
    error: "Arbitrary precision integers do not have a known low.".}
Do NOT use. 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 `or`(x, y: JsBigInt): JsBigInt {.importjs: "(# | #)", ...raises: [], tags: [],
                                      forbids: [].}

Example:

doAssert (big"555" or big"2") == big"555"
Source   Edit  
func `shl`(a, b: JsBigInt): JsBigInt {.importjs: "(# << #)", ...raises: [],
                                       tags: [], forbids: [].}

Example:

doAssert (big"999" shl big"2") == big"3996"
Source   Edit  
func `shr`(a, b: JsBigInt): JsBigInt {.importjs: "(# >> #)", ...raises: [],
                                       tags: [], forbids: [].}

Example:

doAssert (big"999" shr big"2") == big"249"
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 toNumber(this: JsBigInt): int {.importjs: "Number(#)", ...raises: [],
                                     tags: [], forbids: [].}
Does not do any bounds check and may or may not return an inexact representation.

Example:

doAssert toNumber(big"2147483647") == 2147483647.int
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  
func `xor`(x, y: JsBigInt): JsBigInt {.importjs: "(# ^ #)", ...raises: [],
                                       tags: [], forbids: [].}

Example:

doAssert (big"555" xor big"2") == big"553"
Source   Edit