Make div/rem operators because of consistency, minor BigNumbers refactor

This commit is contained in:
Iaroslav Postovalov 2020-07-29 23:44:55 +07:00
parent 0be68cd7d2
commit d7bc0d9f9b
No known key found for this signature in database
GPG Key ID: 70D5F4DCB0972F1B
2 changed files with 11 additions and 10 deletions

View File

@ -87,16 +87,16 @@ interface RemainderDivisionOperations<T> : RingOperations<T> {
/** /**
* Calculates the remainder of dividing this value by [arg]. * Calculates the remainder of dividing this value by [arg].
*/ */
infix fun T.rem(arg: T): T operator fun T.rem(arg: T): T
/** /**
* Performs the floored division of this value by [arg]. * Performs the floored division of this value by [arg].
*/ */
infix fun T.div(arg: T): T operator fun T.div(arg: T): T
override fun binaryOperation(operation: String, left: T, right: T): T = when (operation) { override fun binaryOperation(operation: String, left: T, right: T): T = when (operation) {
REM_OPERATION -> left rem right REM_OPERATION -> left % right
DIV_OPERATION -> left div right DIV_OPERATION -> left / right
else -> super.binaryOperation(operation, left, right) else -> super.binaryOperation(operation, left, right)
} }

View File

@ -14,14 +14,15 @@ object JBigIntegerField : Ring<BigInteger>, RemainderDivisionOperations<BigInteg
override val one: BigInteger override val one: BigInteger
get() = BigInteger.ONE get() = BigInteger.ONE
override fun number(value: Number): BigInteger = BigInteger.valueOf(value.toLong()) override fun number(value: Number): BigInteger = value.toLong().toBigInteger()
override fun add(a: BigInteger, b: BigInteger): BigInteger = a.add(b) override fun add(a: BigInteger, b: BigInteger): BigInteger = a.add(b)
override fun BigInteger.minus(b: BigInteger): BigInteger = this.subtract(b) override fun BigInteger.minus(b: BigInteger): BigInteger = subtract(b)
override fun multiply(a: BigInteger, k: Number): BigInteger = a.multiply(k.toInt().toBigInteger()) override fun multiply(a: BigInteger, k: Number): BigInteger = a.multiply(k.toLong().toBigInteger())
override fun multiply(a: BigInteger, b: BigInteger): BigInteger = a.multiply(b) override fun multiply(a: BigInteger, b: BigInteger): BigInteger = a.multiply(b)
override fun BigInteger.unaryMinus(): BigInteger = negate() override fun BigInteger.unaryMinus(): BigInteger = negate()
override infix fun BigInteger.div(arg: BigInteger): BigInteger = this.divide(arg) override fun BigInteger.div(arg: BigInteger): BigInteger = divide(arg)
override infix fun BigInteger.rem(arg: BigInteger): BigInteger = remainder(arg) override fun BigInteger.div(k: Number): BigInteger = this / k.toLong().toBigInteger()
override fun BigInteger.rem(arg: BigInteger): BigInteger = remainder(arg)
} }
/** /**
@ -38,7 +39,7 @@ abstract class JBigDecimalFieldBase internal constructor(val mathContext: MathCo
override fun add(a: BigDecimal, b: BigDecimal): BigDecimal = a.add(b) override fun add(a: BigDecimal, b: BigDecimal): BigDecimal = a.add(b)
override fun BigDecimal.minus(b: BigDecimal): BigDecimal = subtract(b) override fun BigDecimal.minus(b: BigDecimal): BigDecimal = subtract(b)
override fun number(value: Number): BigDecimal = BigDecimal.valueOf(value.toDouble()) override fun number(value: Number): BigDecimal = value.toDouble().toBigDecimal(mathContext)
override fun multiply(a: BigDecimal, k: Number): BigDecimal = override fun multiply(a: BigDecimal, k: Number): BigDecimal =
a.multiply(k.toDouble().toBigDecimal(mathContext), mathContext) a.multiply(k.toDouble().toBigDecimal(mathContext), mathContext)