Add js BigInts (#16409)

* Add BigInts
* Renames tos plurals
* Improve Stringifications
* Update changelog.md

Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>

* RunnableExamplerize
* discard the discardable pragma
* Several improvements from peer reviews, more docs
* More doc, more test
* More doc, more test
* Better error message 'Error: usage of low is an {.error.} defined at jsbigints.nim' instead of just 'type mismatch JsBigInt'
* is an overload, rename
* proc to scare kids away
* Update lib/js/jsbigints.nim

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

* https://github.com/nim-lang/Nim/pull/16409#discussion_r554365041

Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>
Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
This commit is contained in:
Juan Carlos
2021-01-14 16:19:41 -03:00
committed by GitHub
parent a90f7a66ed
commit 41965880ce
4 changed files with 237 additions and 1 deletions

View File

@@ -0,0 +1,38 @@
discard """
targets: "js"
"""
import std/jsbigints
let big1: JsBigInt = big"2147483647"
let big2: JsBigInt = big"666"
var big3: JsBigInt = big"2"
doAssert big3 == big"2"
doAssert (big3 xor big2) == big"664"
doAssert (big1 mod big2) == big"613"
doAssert -big1 == big"-2147483647"
doAssert big1 div big2 == big"3224449"
doAssert big1 + big2 == big"2147484313"
doAssert big1 - big2 == big"2147482981"
doAssert big1 shl big3 == big"8589934588"
doAssert big1 shr big3 == big"536870911"
doAssert big1 * big2 == big"1430224108902"
doAssert $big1 == "2147483647n"
doAssert big1.toCstring(10) == "2147483647".cstring
doAssert big2 ** big3 == big(443556)
var huge = big"999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"
huge.inc
huge = huge + big"-999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"
doAssert huge == big"1"
var list: seq[JsBigInt]
for i in big"0" .. big"5":
doAssert i is JsBigInt
list.add i
doAssert list == @[big"0", big"1", big"2", big"3", big"4", big"5"]
list = @[]
for i in big"0" ..< big"5":
doAssert i is JsBigInt
list.add i
doAssert list == @[big"0", big"1", big"2", big"3", big"4"]