std/jsbigints
Source EditArbitrary 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
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 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 `div`(x, y: JsBigInt): JsBigInt {.importjs: "(# / #)", ...raises: [], tags: [], forbids: [].}- Same as
divbut forJsBigInt(uses JavaScriptBigInt() / 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
modbut forJsBigInt(uses JavaScriptBigInt() % 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
JsBigInttocstringrepresentation. 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
JsBigInttocstringrepresentation.-
radixBase 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
thisto a signedJsBigIntofbitsbits in-2 ^ (bits - 1)..2 ^ (bits - 1) - 1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/asIntNExample:
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
thisto an unsignedJsBigIntofbitsbits in 0 ..2 ^ bits - 1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/asUintNExample:
doAssert (big("3") + big("2") ** big("66")).wrapToUint(66) == big("3")Source Edit
© 2006–2024 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/jsbigints.html