KEEP-49: extending Kotlin API for BigInteger and BigDecimal

This commit is contained in:
voddan
2017-03-06 13:08:11 +03:00
committed by Ilya Gorbunov
parent 158c03b07f
commit e86e080088
4 changed files with 244 additions and 96 deletions
@@ -0,0 +1,97 @@
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("MathKt")
@file:kotlin.jvm.JvmVersion
package kotlin
import java.math.BigDecimal
import java.math.RoundingMode
/**
* Enables the use of the `+` operator for [BigDecimal] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.plus(other: BigDecimal): BigDecimal = this.add(other)
/**
* Enables the use of the `-` operator for [BigDecimal] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.minus(other: BigDecimal): BigDecimal = this.subtract(other)
/**
* Enables the use of the `*` operator for [BigDecimal] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.times(other: BigDecimal): BigDecimal = this.multiply(other)
/**
* Enables the use of the `/` operator for [BigDecimal] instances.
*
* The scale of the result is the same as the scale of `this` (divident), and for rounding the [RoundingMode.HALF_EVEN]
* rounding mode is used.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.div(other: BigDecimal): BigDecimal = this.divide(other, RoundingMode.HALF_EVEN)
/**
* Enables the use of the `%` operator for [BigDecimal] instances.
*/
@kotlin.internal.InlineOnly
@Deprecated("Use rem(other) instead", ReplaceWith("rem(other)"), DeprecationLevel.WARNING)
public inline operator fun BigDecimal.mod(other: BigDecimal): BigDecimal = this.remainder(other)
/**
* Enables the use of the `%` operator for [BigDecimal] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.rem(other: BigDecimal): BigDecimal = this.remainder(other)
/**
* Enables the use of the unary `-` operator for [BigDecimal] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.unaryMinus(): BigDecimal = this.negate()
/**
* Enables the use of the unary `++` operator for [BigDecimal] instances.
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.inc(): BigDecimal = this.add(BigDecimal.ONE)
/**
* Enables the use of the unary `--` operator for [BigDecimal] instances.
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.dec(): BigDecimal = this.subtract(BigDecimal.ONE)
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun String.toBigDecimal(): BigDecimal = BigDecimal(this)
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Int.toBigDecimal(): BigDecimal = BigDecimal(this)
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Long.toBigDecimal(): BigDecimal = BigDecimal(this)
/*
* JDK documentation recommends using `BigDecimal(String)` instead of `BigDecimal(double)`
* For more details consult http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Float.toBigDecimal(): BigDecimal = BigDecimal(this.toString())
/*
* JDK documentation recommends using `BigDecimal(String)` instead of `BigDecimal(double)`
* For more details consult http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Double.toBigDecimal(): BigDecimal = BigDecimal(this.toString())
@@ -0,0 +1,102 @@
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("MathKt")
@file:kotlin.jvm.JvmVersion
package kotlin
import java.math.BigInteger
import java.math.BigDecimal
/**
* Enables the use of the `+` operator for [BigInteger] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.plus(other: BigInteger): BigInteger = this.add(other)
/**
* Enables the use of the `-` operator for [BigInteger] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.minus(other: BigInteger): BigInteger = this.subtract(other)
/**
* Enables the use of the `*` operator for [BigInteger] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.times(other: BigInteger): BigInteger = this.multiply(other)
/**
* Enables the use of the `/` operator for [BigInteger] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.div(other: BigInteger): BigInteger = this.divide(other)
/**
* Enables the use of the `%` operator for [BigInteger] instances.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.rem(other: BigInteger): BigInteger = this.remainder(other)
/**
* Enables the use of the unary `-` operator for [BigInteger] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.unaryMinus(): BigInteger = this.negate()
/**
* Enables the use of the `++` operator for [BigInteger] instances.
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.inc(): BigInteger = this.add(BigInteger.ONE)
/**
* Enables the use of the `--` operator for [BigInteger] instances.
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.dec(): BigInteger = this.subtract(BigInteger.ONE)
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun BigInteger.inv(): BigInteger = this.not()
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline infix fun BigInteger.and(other: BigInteger): BigInteger = this.and(other)
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline infix fun BigInteger.or(other: BigInteger): BigInteger = this.or(other)
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline infix fun BigInteger.xor(other: BigInteger): BigInteger = this.xor(other)
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline infix fun BigInteger.shl(n: Int): BigInteger = this.shiftLeft(n)
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline infix fun BigInteger.shr(n: Int): BigInteger = this.shiftRight(n)
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun String.toBigInteger(): BigInteger = BigInteger(this)
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Int.toBigInteger(): BigInteger = BigInteger.valueOf(this.toLong())
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Long.toBigInteger(): BigInteger = BigInteger.valueOf(this)
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun BigInteger.toBigDecimal(): BigDecimal = BigDecimal(this)
@@ -1,92 +0,0 @@
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("MathKt")
@file:kotlin.jvm.JvmVersion
package kotlin
import java.math.BigDecimal
import java.math.BigInteger
import java.math.RoundingMode
/**
* Enables the use of the `+` operator for [BigInteger] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.plus(other: BigInteger) : BigInteger = this.add(other)
/**
* Enables the use of the `-` operator for [BigInteger] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.minus(other: BigInteger) : BigInteger = this.subtract(other)
/**
* Enables the use of the `*` operator for [BigInteger] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.times(other: BigInteger) : BigInteger = this.multiply(other)
/**
* Enables the use of the `/` operator for [BigInteger] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.div(other: BigInteger) : BigInteger = this.divide(other)
/**
* Enables the use of the `%` operator for [BigInteger] instances.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.rem(other: BigInteger) : BigInteger = this.remainder(other)
/**
* Enables the use of the unary `-` operator for [BigInteger] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.unaryMinus() : BigInteger = this.negate()
/**
* Enables the use of the `+` operator for [BigDecimal] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.plus(other: BigDecimal) : BigDecimal = this.add(other)
/**
* Enables the use of the `-` operator for [BigDecimal] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.minus(other: BigDecimal) : BigDecimal = this.subtract(other)
/**
* Enables the use of the `*` operator for [BigDecimal] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.times(other: BigDecimal) : BigDecimal = this.multiply(other)
/**
* Enables the use of the `/` operator for [BigDecimal] instances.
*
* The scale of the result is the same as the scale of `this` (divident), and for rounding the [RoundingMode.HALF_EVEN]
* rounding mode is used.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.div(other: BigDecimal) : BigDecimal = this.divide(other, RoundingMode.HALF_EVEN)
/**
* Enables the use of the `%` operator for [BigDecimal] instances.
*/
@kotlin.internal.InlineOnly
@Deprecated("Use rem(other) instead", ReplaceWith("rem(other)"), DeprecationLevel.WARNING)
public inline operator fun BigDecimal.mod(other: BigDecimal) : BigDecimal = this.remainder(other)
/**
* Enables the use of the `%` operator for [BigDecimal] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.rem(other: BigDecimal) : BigDecimal = this.remainder(other)
/**
* Enables the use of the unary `-` operator for [BigDecimal] instances.
*/
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.unaryMinus() : BigDecimal = this.negate()
+45 -4
View File
@@ -20,6 +20,24 @@ class MathTest {
assertEquals(BigInteger("-2"), -a % b)
assertEquals(BigInteger("1"), (-a).mod(b))
assertEquals(BigInteger("-2"), (-a).remainder(b))
assertEquals(BigInteger("3"), a.inc())
assertEquals(BigInteger("1"), a.dec())
assertEquals(BigInteger("-3"), a.inv())
assertEquals(BigInteger("2"), a and b)
assertEquals(BigInteger("3"), a or b)
assertEquals(BigInteger("1"), a xor b)
assertEquals(BigInteger("4"), a shl 1)
assertEquals(BigInteger("1"), a shr 1)
assertEquals(BigInteger("-4"), -a shl 1)
assertEquals(BigInteger("-1"), -a shr 1)
assertEquals(BigInteger("2"), "2".toBigInteger())
assertEquals(BigInteger("-3"), "-3".toBigInteger())
assertEquals(BigInteger("2"), 2.toBigInteger())
assertEquals(BigInteger("-3"), -3L.toBigInteger())
assertEquals(BigDecimal("2"), a.toBigDecimal())
}
@Test fun testBigDecimal() {
@@ -34,10 +52,33 @@ class MathTest {
assertEquals(BigDecimal("-2"), -a % b)
assertEquals(BigDecimal("-2"), (-a).mod(b))
assertEquals(BigDecimal("-2"), (-a).rem(b))
assertEquals(BigDecimal("3"), a.inc())
assertEquals(BigDecimal("1"), a.dec())
assertEquals(BigDecimal("2.5"), "2.5".toBigDecimal())
assertEquals(BigDecimal("-3"), "-3".toBigDecimal())
assertEquals(BigDecimal("2"), 2.toBigDecimal())
assertEquals(BigDecimal("-3"), -3L.toBigDecimal())
assertEquals(BigDecimal("2.0"), 2f.toBigDecimal())
assertEquals(BigDecimal("0.5"), 0.5.toBigDecimal())
}
@Test fun mutatingBigNumbers() {
var a = 2.toBigInteger()
var b = "1.5".toBigDecimal()
a++
b++
assertEquals(BigInteger("3"), a)
assertEquals(BigDecimal("2.5"), b)
--a
--b
assertEquals(BigInteger("2"), a)
assertEquals(BigDecimal("1.5"), b)
}
}
fun main(args: Array<String>) {
MathTest().testBigInteger()
MathTest().testBigDecimal()
}