
Library enables arbitrary precision arithmetic with a unified API, offering type-safe, natural syntax and platform-optimized implementations. Supports advanced math operations and seamless multiplatform functionality.
A Kotlin Multiplatform library for arbitrary precision mathematics, providing unified APIs for high-precision arithmetic operations across Android, iOS, and Web platforms.
KBigDecimal and KBigInteger.gcd, lcm, factorial, sqrt, pow.and, or, xor, not, andNot (with full 2's complement behavior).BigDecimal/BigInteger.+, -, *, /) for intuitive mathematical expressions.Add to your build.gradle.kts:
dependencies {
implementation("io.github.gatrongdev:kbignum:VERSION")
}Add to your build.gradle:
dependencies {
implementation 'io.github.gatrongdev:kbignum:VERSION'
}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:
| Operation | Java (ms) | KBignum (ms) | Relative |
|---|---|---|---|
| Add | 19 | 15 | 0.79x ✓ |
| Subtract | 48 | 38 | 0.79x ✓ |
| Multiply | 62 | 66 | 1.06x |
| Divide | 94 | 90 | 0.96x ✓ |
| Modulo | 28 | 42 | 1.50x |
| Operation | Java (ms) | KBignum (ms) | Relative |
|---|---|---|---|
| Add | 5 | 19 | 3.80x |
| Subtract | 16 | 21 | 1.31x |
| Multiply | 71 | 94 | 1.32x |
| Divide | 29 | 22 | 0.76x ✓ |
| Modulo | 44 | 26 | 0.59x ✓ |
| Operation | Java (ms) | KBignum (ms) | Relative |
|---|---|---|---|
| Factorial(100) | 15 | 9 | 0.60x ✓ |
| Factorial(500) | 22 | 27 | 1.23x |
| Factorial(1000) | 25 | 55 | 2.20x |
| Operation | Java (ms) | KBignum (ms) | Relative |
|---|---|---|---|
| GCD | 1545 | 5403 | 3.50x |
| LCM | 52 | 193 | 3.71x |
| Pow^100 | 50 | 128 | 2.56x |
| Operation | Java (ms) | KBigDecimal (ms) | Relative |
|---|---|---|---|
| Add | 2 | 2 | 1.00x ✓ |
| Sub | 2 | 3 | 1.50x |
| Mul | 1 | 4 | 4.00x |
| Div | 4 | 5 | 1.25x |
| Operation | Java (ms) | KBigDecimal (ms) | Relative |
|---|---|---|---|
| Add | 1 | 1 | 1.00x ✓ |
| Sub | 1 | 1 | 1.00x ✓ |
| Mul | 3 | 6 | 2.00x |
| Div | 3 | 7 | 2.33x |
| Operation | Java (ms) | KBignum (ms) | Relative |
|---|---|---|---|
| Sqrt | 23 | 41 | 1.78x |
| Sqrt | 60 | 43 | 0.72x ✓ |
| Sqrt | 63 | 39 | 0.62x ✓ |
Note: Benchmarks run on macOS/JVM. ✓ indicates KBignum is faster or equal to Java. KBignum prioritizes portability across KMP targets (Android, iOS, JS, Native).
import io.github.gatrongdev.kbignum.math.*
// Creating big numbers
val bigDecimal1 = "123.456789".toKBigDecimal()
val bigDecimal2 = KBigDecimal.fromString("987.654321")
val bigInteger = "12345678901234567890".toKBigInteger()
// Basic arithmetic
val sum = bigDecimal1 + bigDecimal2
val difference = bigDecimal1 - bigDecimal2
val product = bigDecimal1 * bigDecimal2
// Division (Operator '/' defaults to scale 10, HALF_UP)
val quotient = bigDecimal1 / bigDecimal2
// Precise Division
val preciseQuotient = bigDecimal1.divide(bigDecimal2, scale = 20, rounding = KBRoundingMode.HalfUp)
// Advanced operations
val sqrt = KBigMath.sqrt(bigDecimal1, 10)
val factorial = KBigMath.factorial(bigInteger)
val isPrime = KBigMath.isPrime(bigInteger)// Convert from various types
val fromString = "999.999".toKBigDecimal()
val fromInt = 42.toKBigInteger()
val fromLong = 1234567890L.toKBigDecimal()
// Utility functions
val isZero = bigDecimal1.isZero()
val isPositive = bigDecimal1.isPositive()
val absolute = bigDecimal1.abs()
val sign = bigDecimal1.signum()// Mathematical operations
val gcd = KBigMath.gcd(bigInteger1, bigInteger2)
val lcm = KBigMath.lcm(bigInteger1, bigInteger2)
val power = bigDecimal1.pow(5)gradle.properties (android.compileSdk, android.minSdk, android.targetSdk)../gradlew :shared:assembleRelease -Pandroid.compileSdk=34 -Pandroid.targetSdk=34shared/build.gradle.kts are required.NSDecimalNumber bridging overhead.This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.
Gatrong Dev - GitHub
A Kotlin Multiplatform library for arbitrary precision mathematics, providing unified APIs for high-precision arithmetic operations across Android, iOS, and Web platforms.
KBigDecimal and KBigInteger.gcd, lcm, factorial, sqrt, pow.and, or, xor, not, andNot (with full 2's complement behavior).BigDecimal/BigInteger.+, -, *, /) for intuitive mathematical expressions.Add to your build.gradle.kts:
dependencies {
implementation("io.github.gatrongdev:kbignum:VERSION")
}Add to your build.gradle:
dependencies {
implementation 'io.github.gatrongdev:kbignum:VERSION'
}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:
| Operation | Java (ms) | KBignum (ms) | Relative |
|---|---|---|---|
| Add | 19 | 15 | 0.79x ✓ |
| Subtract | 48 | 38 | 0.79x ✓ |
| Multiply | 62 | 66 | 1.06x |
| Divide | 94 | 90 | 0.96x ✓ |
| Modulo | 28 | 42 | 1.50x |
| Operation | Java (ms) | KBignum (ms) | Relative |
|---|---|---|---|
| Add | 5 | 19 | 3.80x |
| Subtract | 16 | 21 | 1.31x |
| Multiply | 71 | 94 | 1.32x |
| Divide | 29 | 22 | 0.76x ✓ |
| Modulo | 44 | 26 | 0.59x ✓ |
| Operation | Java (ms) | KBignum (ms) | Relative |
|---|---|---|---|
| Factorial(100) | 15 | 9 | 0.60x ✓ |
| Factorial(500) | 22 | 27 | 1.23x |
| Factorial(1000) | 25 | 55 | 2.20x |
| Operation | Java (ms) | KBignum (ms) | Relative |
|---|---|---|---|
| GCD | 1545 | 5403 | 3.50x |
| LCM | 52 | 193 | 3.71x |
| Pow^100 | 50 | 128 | 2.56x |
| Operation | Java (ms) | KBigDecimal (ms) | Relative |
|---|---|---|---|
| Add | 2 | 2 | 1.00x ✓ |
| Sub | 2 | 3 | 1.50x |
| Mul | 1 | 4 | 4.00x |
| Div | 4 | 5 | 1.25x |
| Operation | Java (ms) | KBigDecimal (ms) | Relative |
|---|---|---|---|
| Add | 1 | 1 | 1.00x ✓ |
| Sub | 1 | 1 | 1.00x ✓ |
| Mul | 3 | 6 | 2.00x |
| Div | 3 | 7 | 2.33x |
| Operation | Java (ms) | KBignum (ms) | Relative |
|---|---|---|---|
| Sqrt | 23 | 41 | 1.78x |
| Sqrt | 60 | 43 | 0.72x ✓ |
| Sqrt | 63 | 39 | 0.62x ✓ |
Note: Benchmarks run on macOS/JVM. ✓ indicates KBignum is faster or equal to Java. KBignum prioritizes portability across KMP targets (Android, iOS, JS, Native).
import io.github.gatrongdev.kbignum.math.*
// Creating big numbers
val bigDecimal1 = "123.456789".toKBigDecimal()
val bigDecimal2 = KBigDecimal.fromString("987.654321")
val bigInteger = "12345678901234567890".toKBigInteger()
// Basic arithmetic
val sum = bigDecimal1 + bigDecimal2
val difference = bigDecimal1 - bigDecimal2
val product = bigDecimal1 * bigDecimal2
// Division (Operator '/' defaults to scale 10, HALF_UP)
val quotient = bigDecimal1 / bigDecimal2
// Precise Division
val preciseQuotient = bigDecimal1.divide(bigDecimal2, scale = 20, rounding = KBRoundingMode.HalfUp)
// Advanced operations
val sqrt = KBigMath.sqrt(bigDecimal1, 10)
val factorial = KBigMath.factorial(bigInteger)
val isPrime = KBigMath.isPrime(bigInteger)// Convert from various types
val fromString = "999.999".toKBigDecimal()
val fromInt = 42.toKBigInteger()
val fromLong = 1234567890L.toKBigDecimal()
// Utility functions
val isZero = bigDecimal1.isZero()
val isPositive = bigDecimal1.isPositive()
val absolute = bigDecimal1.abs()
val sign = bigDecimal1.signum()// Mathematical operations
val gcd = KBigMath.gcd(bigInteger1, bigInteger2)
val lcm = KBigMath.lcm(bigInteger1, bigInteger2)
val power = bigDecimal1.pow(5)gradle.properties (android.compileSdk, android.minSdk, android.targetSdk)../gradlew :shared:assembleRelease -Pandroid.compileSdk=34 -Pandroid.targetSdk=34shared/build.gradle.kts are required.NSDecimalNumber bridging overhead.This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.
Gatrong Dev - GitHub