/
BigIntegers.kt
129 lines (106 loc) · 4.19 KB
/
BigIntegers.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("MathKt")
package kotlin
import java.math.BigInteger
import java.math.BigDecimal
import java.math.MathContext
/**
* 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)
/** Inverts the bits including the sign bit in this value. */
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun BigInteger.inv(): BigInteger = this.not()
/** Performs a bitwise AND operation between the two values. */
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline infix fun BigInteger.and(other: BigInteger): BigInteger = this.and(other)
/** Performs a bitwise OR operation between the two values. */
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline infix fun BigInteger.or(other: BigInteger): BigInteger = this.or(other)
/** Performs a bitwise XOR operation between the two values. */
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline infix fun BigInteger.xor(other: BigInteger): BigInteger = this.xor(other)
/** Shifts this value left by the [n] number of bits. */
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline infix fun BigInteger.shl(n: Int): BigInteger = this.shiftLeft(n)
/** Shifts this value right by the [n] number of bits, filling the leftmost bits with copies of the sign bit. */
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline infix fun BigInteger.shr(n: Int): BigInteger = this.shiftRight(n)
/**
* Returns the value of this [Int] number as a [BigInteger].
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Int.toBigInteger(): BigInteger = BigInteger.valueOf(this.toLong())
/**
* Returns the value of this [Long] number as a [BigInteger].
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Long.toBigInteger(): BigInteger = BigInteger.valueOf(this)
/**
* Returns the value of this [BigInteger] number as a [BigDecimal].
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun BigInteger.toBigDecimal(): BigDecimal = BigDecimal(this)
/**
* Returns the value of this [BigInteger] number as a [BigDecimal]
* scaled according to the specified [scale] and rounded according to the settings specified with [mathContext].
*
* @param scale the scale of the resulting [BigDecimal], i.e. number of decimal places of the fractional part.
* By default 0.
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun BigInteger.toBigDecimal(scale: Int = 0, mathContext: MathContext = MathContext.UNLIMITED): BigDecimal =
BigDecimal(this, scale, mathContext)