Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C#
* D lang.
* Java
* Nim
* Nim2
  + exercises
* XC=BASIC
* old
  ◇C++, ◇Clojure, ◇Scala


[ edit | logout ]
[ sandbox | passwd ]

bigints

bigints is a 3rd-party library for working with arbitrary precision integers.

$ nimble install bigints

Links:

bigints provides a BigInt type and related operations with standard Nim syntax:

  • creation of BigInt from all standard integer types (initBigInt)
  • comparisons (<, , ==)
  • addition, negation and subtraction (+, -, += -=)
  • multiplication (*, *=)
  • bit shifts (shr, shl)
  • bitwise not, and, or and xor (behave as if negative numbers were represented in 2's complement)
  • integer division and modulo operation (div, mod)
  • conversion of BigInt from/to strings supporting bases from 2 to 36 (initBigInt, $)
  • iteration utilities (inc, dec, countdown, countup, .., ..<)

There's also a pow() function:

func pow(x: BigInt, y: Natural): BigInt

The Natural type means that y cannot be negative.

import bigints

let
  a: BigInt = initBigInt(0)    # 0 as BigInt
  b = initBigInt(2)            # 2 as BigInt
  c = 5.initBigInt             # 5 as BigInt (alternative syntax)
  d = "645347534".initBigInt   # create a BigInt from a string

Example

How much is 2256?

import bigints

let result = pow(2.initBigInt, 256)

echo result

let s: string = $result

echo s

Output:

115792089237316195423570985008687907853269984665640564039457584007913129639936
115792089237316195423570985008687907853269984665640564039457584007913129639936

As you can see, a BigInt can be converted to a string with the `$` operator.

Notes

  • I know that in D Lang, BigInt performance is optimized for numbers below ~1000 decimal digits. I'm not sure but I think it's similar in Nim too.
  • I tried the pow() function to produce a really huge number and it was very slow. Interestingly, for very large numbers, Python performs much better.
  • For very large numbers, consider using the GMP library instead. There are Nim bindings to it but I haven't tried them yet.
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 April 23, 22:25