std/jsbigints (original) (raw)
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"
func 'big(num: cstring): JsBigInt {.importjs: "BigInt(#)", ...raises: [],
tags: [], forbids: [].}
Constructor for JsBigInt.
Example:
doAssert -1'big == 1'big - 2'big
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))
func *(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)", ...raises: [], tags: [],
forbids: [].}
Example:
doAssert (big"42" * big"9") == big"378"
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" doAssert big"0" ** big"0" == big"1" var ok = false try: discard big"2" ** big"-1" except: ok = true doAssert ok
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"
func +(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)", ...raises: [], tags: [],
forbids: [].}
Example:
doAssert (big"9" + big"1") == big"10"
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"
func -(this: JsBigInt): JsBigInt {.importjs: "($1#)", ...raises: [], tags: [],
forbids: [].}
Example:
doAssert -(big"10101010101") == big"-10101010101"
func -(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)", ...raises: [], tags: [],
forbids: [].}
Example:
doAssert (big"9" - big"1") == big"8"
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"
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"
func <(x, y: JsBigInt): bool {.importjs: "(# $1 #)", ...raises: [], tags: [],
forbids: [].}
Example:
doAssert big"2" < big"9"
func <=(x, y: JsBigInt): bool {.importjs: "(# $1 #)", ...raises: [], tags: [],
forbids: [].}
Example:
doAssert big"1" <= big"5"
func ==(x, y: JsBigInt): bool {.importjs: "(# == #)", ...raises: [], tags: [],
forbids: [].}
Example:
doAssert big"42" == big"42"
func and(x, y: JsBigInt): JsBigInt {.importjs: "(# & #)", ...raises: [],
tags: [], forbids: [].}
Example:
doAssert (big"555" and big"2") == big"2"
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
func dec(this: var JsBigInt) {.importjs: "(--[#][0][0])", ...raises: [], tags: [], forbids: [].}
Example:
var big1: JsBigInt = big"2" dec big1 doAssert big1 == big"1"
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"
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"
func inc(this: var JsBigInt) {.importjs: "(++[#][0][0])", ...raises: [], tags: [], forbids: [].}
Example:
var big1: JsBigInt = big"1" inc big1 doAssert big1 == big"2"
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"
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"
func or(x, y: JsBigInt): JsBigInt {.importjs: "(# | #)", ...raises: [], tags: [],
forbids: [].}
Example:
doAssert (big"555" or big"2") == big"555"
func shl(a, b: JsBigInt): JsBigInt {.importjs: "(# << #)", ...raises: [],
tags: [], forbids: [].}
Example:
doAssert (big"999" shl big"2") == big"3996"
func shr(a, b: JsBigInt): JsBigInt {.importjs: "(# >> #)", ...raises: [],
tags: [], forbids: [].}
Example:
doAssert (big"999" shr big"2") == big"249"
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
func xor(x, y: JsBigInt): JsBigInt {.importjs: "(# ^ #)", ...raises: [],
tags: [], forbids: [].}
Example:
doAssert (big"555" xor big"2") == big"553"