Merge SCI-MR-10 from improved-java-math-big-numbers

This commit is contained in:
Alexander Nozik 2020-07-29 08:39:19 +00:00
commit c2ca7b871d
2 changed files with 30 additions and 12 deletions

View File

@ -340,7 +340,7 @@ internal class AsmBuilder<T> internal constructor(
checkcast(type) checkcast(type)
} }
fun loadNumeric(value: Number) { internal fun loadNumeric(value: Number) {
if (expectationStack.peek() == NUMBER_TYPE) { if (expectationStack.peek() == NUMBER_TYPE) {
loadNumberConstant(value, true) loadNumberConstant(value, true)
expectationStack.pop() expectationStack.pop()

View File

@ -8,30 +8,48 @@ import java.math.MathContext
* A field wrapper for Java [BigInteger] * A field wrapper for Java [BigInteger]
*/ */
object JBigIntegerField : Field<BigInteger> { object JBigIntegerField : Field<BigInteger> {
override val zero: BigInteger = BigInteger.ZERO override val zero: BigInteger
override val one: BigInteger = BigInteger.ONE get() = BigInteger.ZERO
override fun add(a: BigInteger, b: BigInteger): BigInteger = a.add(b) override val one: BigInteger
get() = BigInteger.ONE
override fun multiply(a: BigInteger, k: Number): BigInteger = a.multiply(k.toInt().toBigInteger())
override fun multiply(a: BigInteger, b: BigInteger): BigInteger = a.multiply(b)
override fun number(value: Number): BigInteger = BigInteger.valueOf(value.toLong())
override fun divide(a: BigInteger, b: BigInteger): BigInteger = a.div(b) override fun divide(a: BigInteger, b: BigInteger): BigInteger = a.div(b)
override fun add(a: BigInteger, b: BigInteger): BigInteger = a.add(b)
override fun BigInteger.minus(b: BigInteger): BigInteger = this.subtract(b)
override fun multiply(a: BigInteger, k: Number): BigInteger = a.multiply(k.toInt().toBigInteger())
override fun multiply(a: BigInteger, b: BigInteger): BigInteger = a.multiply(b)
override fun BigInteger.unaryMinus(): BigInteger = negate()
} }
/** /**
* A Field wrapper for Java [BigDecimal] * A Field wrapper for Java [BigDecimal]
*/ */
class JBigDecimalField(val mathContext: MathContext = MathContext.DECIMAL64) : Field<BigDecimal> { abstract class JBigDecimalFieldBase internal constructor(val mathContext: MathContext = MathContext.DECIMAL64) :
override val zero: BigDecimal = BigDecimal.ZERO Field<BigDecimal>,
override val one: BigDecimal = BigDecimal.ONE PowerOperations<BigDecimal> {
override val zero: BigDecimal
get() = BigDecimal.ZERO
override val one: BigDecimal
get() = BigDecimal.ONE
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 number(value: Number): BigDecimal = BigDecimal.valueOf(value.toDouble())
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)
override fun multiply(a: BigDecimal, b: BigDecimal): BigDecimal = a.multiply(b, mathContext) override fun multiply(a: BigDecimal, b: BigDecimal): BigDecimal = a.multiply(b, mathContext)
override fun divide(a: BigDecimal, b: BigDecimal): BigDecimal = a.divide(b, mathContext) override fun divide(a: BigDecimal, b: BigDecimal): BigDecimal = a.divide(b, mathContext)
override fun power(arg: BigDecimal, pow: Number): BigDecimal = arg.pow(pow.toInt(), mathContext)
override fun sqrt(arg: BigDecimal): BigDecimal = arg.sqrt(mathContext)
override fun BigDecimal.unaryMinus(): BigDecimal = negate(mathContext)
}
class JBigDecimalField(mathContext: MathContext = MathContext.DECIMAL64) : JBigDecimalFieldBase(mathContext) {
companion object : JBigDecimalFieldBase()
} }