KBigNum

A Kotlin Multiplatform library for arbitrary precision mathematics, providing unified APIs for high-precision arithmetic operations across Android, iOS, and Web platforms.
Features
Installation
Gradle (Kotlin DSL)
Add to your build.gradle.kts:
dependencies {
implementation("io.github.gatrongdev:kbignum:VERSION")
}
Gradle (Groovy)
Add to your build.gradle:
dependencies {
implementation 'io.github.gatrongdev:kbignum:VERSION'
}
Performance
KBignum offers competitive performance by utilizing efficient algorithms (Knuth's Algorithm D for division, optimized magnitude arithmetic). Below is a comparison against Java's native implementations on JVM:
KBigInteger
2048-bit Numbers
4096-bit Numbers
Factorial
KBigMath (Integer)
KBigDecimal
600-digit (~2000 bits)
1200-digit (~4000 bits)
KBigMath (Decimal)
Note: Benchmarks run on macOS/JVM. ✓ indicates KBignum is faster or equal to Java. KBignum prioritizes portability across KMP targets (Android, iOS, JS, Native).
Quick Start
Basic Usage
import io.github.gatrongdev.kbignum.math.*
bigDecimal1 = .toKBigDecimal()
bigDecimal2 = KBigDecimal.fromString()
bigInteger = .toKBigInteger()
sum = bigDecimal1 + bigDecimal2
difference = bigDecimal1 - bigDecimal2
product = bigDecimal1 * bigDecimal2
quotient = bigDecimal1 / bigDecimal2
preciseQuotient = bigDecimal1.divide(bigDecimal2, scale = , rounding = KBRoundingMode.HalfUp)
sqrt = KBigMath.sqrt(bigDecimal1, )
factorial = KBigMath.factorial(bigInteger)
isPrime = KBigMath.isPrime(bigInteger)
Extension Functions
val fromString = "999.999".toKBigDecimal()
val fromInt = 42.toKBigInteger()
val fromLong = 1234567890L.toKBigDecimal()
val isZero = bigDecimal1.isZero()
val isPositive = bigDecimal1.isPositive()
val absolute = bigDecimal1.abs()
val sign = bigDecimal1.signum()
Advanced Mathematics
val gcd = KBigMath.gcd(bigInteger1, bigInteger2)
val lcm = KBigMath.lcm(bigInteger1, bigInteger2)
val power = bigDecimal1.pow(5)
Platform Support
Android
- Minimum API Level: 21
- Implementation: Pure Kotlin (Single Codebase). No JNI involved.
- Output: AAR library files
Android SDK configuration
- SDK levels are centralized in the root
gradle.properties (android.compileSdk, android.minSdk, android.targetSdk).
- Override them per build by supplying Gradle properties, e.g.:
iOS
- Minimum Version: 13.0
- Implementation: Pure Kotlin (Single Codebase). No
NSDecimalNumber bridging overhead.
- Output: XCFramework with arm64 and x64 support
Requirements
- Kotlin: 1.9.10+
- Gradle: 8.11.1+
- Android: API level 21+
- iOS: 13.0+
License
This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.
Author
Gatrong Dev - GitHub