diff --git a/examples/src/benchmarks/kotlin/scientifik/kmath/bignum/BigDecimalBenchmark.kt b/examples/src/benchmarks/kotlin/scientifik/kmath/bignum/BigDecimalBenchmark.kt new file mode 100644 index 000000000..12dd060c0 --- /dev/null +++ b/examples/src/benchmarks/kotlin/scientifik/kmath/bignum/BigDecimalBenchmark.kt @@ -0,0 +1,41 @@ +package scientifik.kmath.bignum + +import org.openjdk.jmh.annotations.Benchmark +import org.openjdk.jmh.annotations.Scope +import org.openjdk.jmh.annotations.State +import scientifik.kmath.operations.JBigDecimalField +import scientifik.kmath.operations.invoke +import kotlin.random.Random + +@State(Scope.Benchmark) +class BigDecimalBenchmark { + final var times: Int = 10000 + + @Benchmark + fun java() { + val random = Random(0) + var sum = JBigDecimalField.zero + + repeat(times) { + sum += JBigDecimalField { + number(random.nextDouble(-1000000000.0, 1000000000.0)) / random.nextDouble(-1000000.0, 1000000.0) + } + } + + println("java:$sum") + } + + @Benchmark + fun bignum() { + val random = Random(0) + var sum = BigDecimalField.zero + + repeat(times) { + sum += BigDecimalField { + number(random.nextDouble(-1000000000.0, 1000000000.0)) / random.nextDouble(-1000000.0, 1000000.0) + } + } + + println("bignum:$sum") + } +} diff --git a/examples/src/benchmarks/kotlin/scientifik/kmath/bignum/BigIntegerBenchmark.kt b/examples/src/benchmarks/kotlin/scientifik/kmath/bignum/BigIntegerBenchmark.kt index fca4e22ba..d9a609016 100644 --- a/examples/src/benchmarks/kotlin/scientifik/kmath/bignum/BigIntegerBenchmark.kt +++ b/examples/src/benchmarks/kotlin/scientifik/kmath/bignum/BigIntegerBenchmark.kt @@ -10,7 +10,7 @@ import kotlin.random.Random @State(Scope.Benchmark) class BigIntegerBenchmark { - var times: Int = 1000000 + final var times: Int = 1000000 @Benchmark fun java() { diff --git a/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigDecimalField.kt b/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigDecimalField.kt index 507c8c163..ddca93dd7 100644 --- a/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigDecimalField.kt +++ b/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigDecimalField.kt @@ -2,9 +2,10 @@ package scientifik.kmath.bignum import com.ionspin.kotlin.bignum.decimal.BigDecimal import com.ionspin.kotlin.bignum.decimal.DecimalMode +import com.ionspin.kotlin.bignum.decimal.RoundingMode import scientifik.kmath.operations.Field -abstract class BigDecimalFieldBase internal constructor(val mode: DecimalMode = DecimalMode.DEFAULT) : +abstract class BigDecimalFieldBase internal constructor(val mode: DecimalMode = DEFAULT_MODE) : Field { override val zero: BigDecimal get() = BigDecimal.ZERO @@ -17,12 +18,17 @@ abstract class BigDecimalFieldBase internal constructor(val mode: DecimalMode = override fun multiply(a: BigDecimal, k: Number): BigDecimal = a.times(number(k)) override fun multiply(a: BigDecimal, b: BigDecimal): BigDecimal = a.times(b) override fun divide(a: BigDecimal, b: BigDecimal): BigDecimal = a.divide(b) - override fun BigDecimal.times(k: Number): BigDecimal = times(number(k)) - override fun BigDecimal.plus(b: Number): BigDecimal = plus(number(b)) - override fun BigDecimal.minus(b: Number): BigDecimal = minus(number(b)) - override fun BigDecimal.div(k: Number): BigDecimal = div(number(k)) + override fun BigDecimal.minus(b: BigDecimal): BigDecimal = subtract(b, mode) + override fun BigDecimal.times(k: Number): BigDecimal = multiply(number(k), mode) + override fun BigDecimal.plus(b: Number): BigDecimal = add(number(b), mode) + override fun BigDecimal.minus(b: Number): BigDecimal = subtract(number(b), mode) + override fun BigDecimal.div(k: Number): BigDecimal = divide(number(k), mode) + + companion object { + internal val DEFAULT_MODE = DecimalMode(16, RoundingMode.ROUND_HALF_AWAY_FROM_ZERO) + } } -class BigDecimalField(mode: DecimalMode = DecimalMode.DEFAULT) : BigDecimalFieldBase(mode) { +class BigDecimalField(mode: DecimalMode = DEFAULT_MODE) : BigDecimalFieldBase(mode) { companion object : BigDecimalFieldBase() }