Add decimal wrapper

This commit is contained in:
Iaroslav Postovalov 2020-09-13 16:31:18 +07:00
parent dc02d957b0
commit 855bab258a
No known key found for this signature in database
GPG Key ID: 70D5F4DCB0972F1B
2 changed files with 33 additions and 5 deletions

View File

@ -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<BigDecimal> {
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()
}

View File

@ -12,10 +12,10 @@ object BigIntegerRing : Ring<BigInteger> {
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())
}