gmp
| Kind | ffi-c |
|---|---|
| Capabilities | ffi |
| Categories | math ffi |
| Keywords | gmp bignum arbitrary-precision math integer |
GMP arbitrary-precision arithmetic library bindings for Kit
Files
| File | Description |
|---|---|
.editorconfig | Editor formatting configuration |
.gitignore | Git ignore rules for build artifacts and dependencies |
.tool-versions | asdf tool versions (Zig, Kit) |
LICENSE | MIT license file |
README.md | This file |
c/kit_gmp.c | C FFI wrapper |
c/kit_gmp.h | C header for FFI wrapper |
examples/basic.kit | Basic usage example |
kit.toml | Package manifest with metadata and dependencies |
src/gmp.kit | Kit GMP - Arbitrary-precision arithmetic library bindings |
tests/gmp.test.kit | Tests for gmp |
Dependencies
No Kit package dependencies.
Installation
kit add gitlab.com/kit-lang/packages/kit-gmp.gitSystem Requirements
| Platform | Command |
|---|---|
| macOS | brew install gmp |
| Ubuntu | sudo apt install libgmp-dev |
| Fedora | sudo dnf install gmp-devel |
Usage
import Kit.GMP as Gmp
main = fn =>
println ("GMP version: " ++ Gmp.version)
# Big integer arithmetic
a = Gmp.int-from-int 42
b = Gmp.int-from-int 58
c = Gmp.int-add a b
println ("42 + 58 = " ++ (Gmp.int-to-string c))
Gmp.int-free c
Gmp.int-free b
Gmp.int-free a
# Very large numbers from strings
match Gmp.int-from-string "123456789012345678901234567890"
| Ok big ->
println ("Big number: " ++ (Gmp.int-to-string big))
Gmp.int-free big
| Err e -> println ("Parse error: " ++ e)
# Factorial
fact100 = Gmp.int-factorial 100
println ("100! = " ++ (Gmp.int-to-string fact100))
Gmp.int-free fact100
# Power
base-val = Gmp.int-from-int 2
big-pow = Gmp.int-pow base-val 256
println ("2^256 = " ++ (Gmp.int-to-string big-pow))
Gmp.int-free big-pow
Gmp.int-free base-val
# Rational arithmetic
match Gmp.rat-from-ints 1 3
| Ok one-third ->
match Gmp.rat-from-ints 1 6
| Ok one-sixth ->
total = Gmp.rat-add one-third one-sixth
println ("1/3 + 1/6 = " ++ (Gmp.rat-to-string total))
Gmp.rat-free total
Gmp.rat-free one-sixth
| Err e -> println ("Error: " ++ e)
Gmp.rat-free one-third
| Err e -> println ("Error: " ++ e)
mainDevelopment
Running Examples
Run examples with the interpreter:
kit run examples/basic.kitCompile examples to a native binary:
kit build examples/basic.kit && ./basicRunning Tests
Run the test suite:
kit testRun the test suite with coverage:
kit test --coverageRunning kit dev
Run the standard development workflow (format, check, test):
kit devThis will:
- Format and check source files in
src/ - Run tests in
tests/with coverage
Generating Documentation
Generate API documentation from doc comments:
kit docNote: Kit sources with doc comments (##) will generate HTML documents in docs/*.html
Cleaning Build Artifacts
Remove generated files, caches, and build artifacts:
kit task cleanNote: Defined in kit.toml.
Local Installation
To install this package locally for development:
kit installThis installs the package to ~/.kit/packages/@kit/gmp/, making it available for import as Kit.GMP in other projects.
License
This package is released under the MIT License - see LICENSE for details.
GMP is released under the LGPL 3.0 / GPL 2.0 License.
Exported Functions & Types
int-from-string
Create a big integer from a decimal string.
Parameters:
Returns:
String -> Result Ptr String
int-from-string-base
Create a big integer from a string in the given base.
Parameters:
Returns:
String -> Int -> Result Ptr String
int-from-int
Create a big integer from a native integer.
Parameters:
Returns:
Int -> Ptr
int-to-string
Convert a big integer to a decimal string.
Parameters:
Returns:
Ptr -> String
int-to-string-base
Convert a big integer to a string in the given base.
Parameters:
Returns:
Ptr -> Int -> String
int-free
Free a big integer and release its memory.
Parameters:
Ptr -> Unit
int-add
Add two big integers.
Ptr -> Ptr -> Ptr
int-sub
Subtract two big integers (a - b).
Ptr -> Ptr -> Ptr
int-mul
Multiply two big integers.
Ptr -> Ptr -> Ptr
int-div
Divide two big integers (truncating division).
Returns:
Ptr -> Ptr -> Result Ptr String
int-mod
Compute a modulo b.
Returns:
Ptr -> Ptr -> Result Ptr String
int-pow
Raise a big integer to a power.
Parameters:
Ptr -> Int -> Ptr
int-neg
Negate a big integer.
Ptr -> Ptr
int-abs
Absolute value of a big integer.
Ptr -> Ptr
int-factorial
Compute n! (factorial).
Parameters:
Int -> Ptr
int-gcd
Compute the greatest common divisor of two big integers.
Ptr -> Ptr -> Ptr
int-cmp
Compare two big integers.
Returns:
Ptr -> Ptr -> Int
int-eq?
Check if two big integers are equal.
Ptr -> Ptr -> Bool
int-lt?
Check if a < b.
Ptr -> Ptr -> Bool
int-gt?
Check if a > b.
Ptr -> Ptr -> Bool
int-sign
Get the sign of a big integer (-1, 0, or 1).
Ptr -> Int
int-is-zero?
Check if a big integer is zero.
Ptr -> Bool
int-bit-length
Get the number of bits needed to represent the integer.
Ptr -> Int
rat-from-string
Create a rational number from a string (e.g., "3/4" or "1/3").
Returns:
String -> Result Ptr String
rat-from-ints
Create a rational number from a numerator and denominator.
Returns:
Int -> Int -> Result Ptr String
rat-to-string
Convert a rational number to a string (e.g., "3/4").
Ptr -> String
rat-free
Free a rational number.
Ptr -> Unit
rat-add
Add two rational numbers.
Ptr -> Ptr -> Ptr
rat-sub
Subtract two rational numbers.
Ptr -> Ptr -> Ptr
rat-mul
Multiply two rational numbers.
Ptr -> Ptr -> Ptr
rat-div
Divide two rational numbers.
Returns:
Ptr -> Ptr -> Result Ptr String
rat-cmp
Compare two rational numbers.
Ptr -> Ptr -> Int
rat-eq?
Check if two rationals are equal.
Ptr -> Ptr -> Bool
version
Get the GMP library version string.
String