From 855bab258a48a20e1eadcf156b1cc83b5828908f Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Sun, 13 Sep 2020 16:31:18 +0700 Subject: [PATCH] Add decimal wrapper --- .../kmath/bignum/BigDecimalField.kt | 28 +++++++++++++++++++ .../scientifik/kmath/bignum/BigIntegerRing.kt | 10 +++---- 2 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigDecimalField.kt diff --git a/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigDecimalField.kt b/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigDecimalField.kt new file mode 100644 index 000000000..507c8c163 --- /dev/null +++ b/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigDecimalField.kt @@ -0,0 +1,28 @@ +package scientifik.kmath.bignum + +import com.ionspin.kotlin.bignum.decimal.BigDecimal +import com.ionspin.kotlin.bignum.decimal.DecimalMode +import scientifik.kmath.operations.Field + +abstract class BigDecimalFieldBase internal constructor(val mode: DecimalMode = DecimalMode.DEFAULT) : + Field { + override val zero: BigDecimal + get() = BigDecimal.ZERO + + override val one: BigDecimal + get() = BigDecimal.ONE + + override fun number(value: Number): BigDecimal = BigDecimal.fromDouble(value.toDouble(), mode) + override fun add(a: BigDecimal, b: BigDecimal): BigDecimal = a.add(b, mode) + 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)) +} + +class BigDecimalField(mode: DecimalMode = DecimalMode.DEFAULT) : BigDecimalFieldBase(mode) { + companion object : BigDecimalFieldBase() +} diff --git a/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigIntegerRing.kt b/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigIntegerRing.kt index 3608256b9..3c573ccf3 100644 --- a/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigIntegerRing.kt +++ b/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigIntegerRing.kt @@ -12,10 +12,10 @@ object BigIntegerRing : Ring { override fun number(value: Number): BigInteger = BigInteger.fromLong(value.toLong()) override fun add(a: BigInteger, b: BigInteger): BigInteger = a + b - override fun multiply(a: BigInteger, k: Number): BigInteger = a * (number(k)) + override fun multiply(a: BigInteger, k: Number): BigInteger = a * k.toLong() override fun multiply(a: BigInteger, b: BigInteger): BigInteger = a * b - override fun BigInteger.plus(b: Number): BigInteger = plus(number(b)) - override fun BigInteger.minus(b: Number): BigInteger = minus(number(b)) - override fun BigInteger.div(k: Number): BigInteger = divide(number(k)) - override fun BigInteger.times(k: Number): BigInteger = multiply(number(k)) + override fun BigInteger.plus(b: Number): BigInteger = plus(b.toLong()) + override fun BigInteger.minus(b: Number): BigInteger = minus(b.toLong()) + override fun BigInteger.div(k: Number): BigInteger = div(k.toLong()) + override fun BigInteger.times(k: Number): BigInteger = times(k.toLong()) }