Kt-Math
Arbitrary-precision integers, decimals and rationals for Kotlin Multiplatform: the API of Java's
java.math.BigInteger and java.math.BigDecimal, plus BigRational and a library of mathematical functions, on the
JVM, JavaScript and every Kotlin/Native target, usable from Kotlin, Java, JavaScript and C.
import org.gciatto.kt.math.*
val x = BigInteger.of("123456789012345678901234567890")
x * 42.toBigInteger() // 5185185138518518513851851851380
"1.10".toBigDecimal().div(3.toBigDecimal(), MathContext(10)) // 0.3666666667
("1/3".toBigRational() + BigRational.valueOf(1, 6)).reduce() // 1/2
Why Kt-Math
Kotlin's standard library has no arbitrary-precision numbers outside the JVM. Kt-Math brings java.math's well-known
API to common Kotlin code, so the same code computes with big numbers everywhere, and gets the same results:
- The
java.mathAPI, everywhere.BigIntegerandBigDecimalmirror their Java namesakes (arithmetic, bit operations,modPow, primality, scales,MathContext, the eightRoundingModes,sqrt, ...), with Kotlin operators on top. - The fastest implementation of each platform. On the JVM, Kt-Math delegates to
java.mathitself; on JavaScript,BigIntegers are nativeBigInts; on Kotlin/Native, a line-by-line port of the JDK, with C kernels for the hot loops of multiplication andmodPow. - The same results on every platform. The Kotlin port is tested differentially: thousands of operations run on
java.mathon the JVM, and every platform must reproduce their results, and their exceptions, exactly. - More than
java.math. ExactBigRationals;BigFactory, with parsing functions returningnullon invalid input; conversions among all number types; and, inorg.gciatto.kt.math.big, logarithms, powers, roots, trigonometric and hyperbolic functions, gamma, π and e at any precision, complex numbers and decimal ranges (a port of big-math). - Four languages. An idiomatic Kotlin API, Java-friendly static factories (Java 8+), an npm package with JS names for every overload, and a C API through Kotlin/Native shared libraries.
Getting started
| Coordinates | |
|---|---|
| Gradle (Kotlin Multiplatform or JVM) | implementation("io.github.gciatto:kt-math:VERSION") |
| Maven | io.github.gciatto:kt-math-jvm:VERSION |
| npm | npm install kt-math |
| C | the shared library and header of each GitHub release (Linux, macOS, Windows) |
Then follow a tutorial for Kotlin, Java, JavaScript or C. The API reference documents every class and function.
Platforms
| Platform | Targets | BigInteger |
BigDecimal |
|---|---|---|---|
| JVM | Java 8+ | java.math.BigInteger |
java.math.BigDecimal |
| JavaScript | Node.js, browsers | native BigInt |
Kotlin port, on BigInts |
| Kotlin/Native | macOS, iOS, watchOS, tvOS, Linux, Windows, Android NDK: every non-deprecated target | Kotlin port, with C kernels | Kotlin port |
BigRational, BigComplex and BigDecimalMath are written in Kotlin on top of the platform's numbers. See the
platforms reference for the full list of targets.
Performance
Average time per operation, as printed by ./gradlew benchmark (JVM 17, Node.js 22, and a Kotlin/Native
linuxArm64 release binary in a Linux container, all on the same Apple Silicon laptop; lower is better):
| Operation | JVM (java.math) |
JavaScript (BigInt) |
Kotlin/Native |
|---|---|---|---|
| 2048 × 2048 bits product | 1.1 µs | 10.9 µs | 1.2 µs |
| 8192 × 8192 bits product | 16.8 µs | 70.7 µs | 29.1 µs |
| 8192 / 2048 bits division | 43.6 µs | 25.7 µs | 33.5 µs |
8192 bits toString |
125 µs | 44 µs | 160 µs |
2048 bits modPow |
3.0 ms | 11.7 ms | 4.7 ms |
4096 bits modPow |
23.8 ms | 65.5 ms | 37.4 ms |
fibonacci(10000) (additions) |
2.1 ms | 0.7 ms | 3.0 ms |
1000! (products by small integers) |
262 µs | 113 µs | 319 µs |
| decimal division, 100 digits | 1.2 µs | 2.7 µs | 1.8 µs |
decimal sqrt, 1000 digits |
594 µs | 782 µs | 778 µs |
On Kotlin/Native, the C kernels make multiplication and modPow up to 10 times faster than the plain Kotlin port.
Run ./gradlew benchmark to reproduce the numbers on your machine.
Development
Kt-Math is built with Gradle (JDK 17+); ./gradlew check runs the style checks and the tests the host can run, and
./gradlew :documentation:assembleSite builds the website. See
how to contribute and AGENTS.md for the details. Releases
are automatic, from conventional commits.
License
Kt-Math is a port of OpenJDK's java.math, hence distributed under the same license: the
GNU General Public License, version 2, with the Classpath Exception, which allows linking it into software
under any license. The org.gciatto.kt.math.big package and BigRational port big-math,
under the MIT license.