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()