From e86e080088c7f31db17254ce1f6a7386703cb8ae Mon Sep 17 00:00:00 2001 From: voddan Date: Mon, 6 Mar 2017 13:08:11 +0300 Subject: [PATCH] KEEP-49: extending Kotlin API for BigInteger and BigDecimal --- .../stdlib/src/kotlin/util/BigDecimals.kt | 97 +++++++++++++++++ .../stdlib/src/kotlin/util/BigIntegers.kt | 102 ++++++++++++++++++ .../stdlib/src/kotlin/util/BigNumbers.kt | 92 ---------------- libraries/stdlib/test/numbers/MathJVMTest.kt | 49 ++++++++- 4 files changed, 244 insertions(+), 96 deletions(-) create mode 100644 libraries/stdlib/src/kotlin/util/BigDecimals.kt create mode 100644 libraries/stdlib/src/kotlin/util/BigIntegers.kt delete mode 100644 libraries/stdlib/src/kotlin/util/BigNumbers.kt diff --git a/libraries/stdlib/src/kotlin/util/BigDecimals.kt b/libraries/stdlib/src/kotlin/util/BigDecimals.kt new file mode 100644 index 00000000000..be1c6e696c9 --- /dev/null +++ b/libraries/stdlib/src/kotlin/util/BigDecimals.kt @@ -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()) \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/util/BigIntegers.kt b/libraries/stdlib/src/kotlin/util/BigIntegers.kt new file mode 100644 index 00000000000..f2d88713771 --- /dev/null +++ b/libraries/stdlib/src/kotlin/util/BigIntegers.kt @@ -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) \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/util/BigNumbers.kt b/libraries/stdlib/src/kotlin/util/BigNumbers.kt deleted file mode 100644 index 5af467f7e2e..00000000000 --- a/libraries/stdlib/src/kotlin/util/BigNumbers.kt +++ /dev/null @@ -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() diff --git a/libraries/stdlib/test/numbers/MathJVMTest.kt b/libraries/stdlib/test/numbers/MathJVMTest.kt index 16d54f8a408..fbe6208ed19 100644 --- a/libraries/stdlib/test/numbers/MathJVMTest.kt +++ b/libraries/stdlib/test/numbers/MathJVMTest.kt @@ -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) { - MathTest().testBigInteger() - MathTest().testBigDecimal() -}