KBigInt

An arbitrary-precision integer: what BigInt values are made of.

Upstream uses java.math.BigInteger, which common Kotlin has no equal of, so this is the port's own (D-5). Sign and magnitude, with the magnitude a little-endian IntArray of base 2^32 limbs and no trailing zero limbs, so a value has exactly one representation and equality is a plain array comparison.

The bitwise operations follow the JavaScript rule: two's complement over an infinitely long bit string, so -1n and 3n is 3n and not(5n) is -6n. shiftRight floors, matching >>.

It deliberately does NOT extend Number, even though upstream's BigInteger does: on Kotlin/JS Number is the JS primitive number type, so a class extending it fails is Number and throws on as Number. The runtime tells a bigint apart with is KBigInt instead (D-54).

Constructors

Link copied to clipboard
constructor(digits: String, radix: Int)

Parses digits in radix. This is the shape the lexer builds a 123n literal with.

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Functions

Link copied to clipboard
fun abs(): KBigInt
Link copied to clipboard
fun add(other: KBigInt): KBigInt
Link copied to clipboard
fun and(other: KBigInt): KBigInt
Link copied to clipboard
fun asIntN(bits: Int): KBigInt

BigInt.asIntN: the low bits bits, read as a two's complement signed number.

Link copied to clipboard
fun asUintN(bits: Int): KBigInt

BigInt.asUintN: the low bits bits, read as a number that is never negative.

Link copied to clipboard
fun bitLength(): Int

The bits in the shortest two's complement spelling, not counting the sign bit, the same count java.math.BigInteger.bitLength gives.

Link copied to clipboard
open operator override fun compareTo(other: KBigInt): Int
Link copied to clipboard

Compares against a finite d without losing anything to rounding.

Link copied to clipboard
fun divide(other: KBigInt): KBigInt

Truncates toward zero, which is what / does on BigInt.

Link copied to clipboard
Link copied to clipboard
open operator override fun equals(other: Any?): Boolean
Link copied to clipboard
open override fun hashCode(): Int
Link copied to clipboard

The value as an Int, or an error when it does not fit.

Link copied to clipboard

The value as a Long, or an error when it does not fit.

Link copied to clipboard
fun mod(modulus: KBigInt): KBigInt

The remainder that is never negative, so (-7).mod(3) is 2.

Link copied to clipboard
fun multiply(other: KBigInt): KBigInt
Link copied to clipboard
Link copied to clipboard
fun not(): KBigInt

-x - 1, which is what ~ means on an infinitely long two's complement number.

Link copied to clipboard
fun or(other: KBigInt): KBigInt
Link copied to clipboard
fun pow(exponent: Int): KBigInt
Link copied to clipboard

Takes the sign of the dividend, which is what % does on BigInt.

Link copied to clipboard
Link copied to clipboard

Arithmetic, so it floors: -5 shr 1 is -3, not -2.

Link copied to clipboard
fun signum(): Int

-1, 0 or 1.

Link copied to clipboard
fun subtract(other: KBigInt): KBigInt
Link copied to clipboard
Link copied to clipboard
fun toByte(): Byte
Link copied to clipboard

The nearest double, rounded to even, and infinite once past the double range.

Link copied to clipboard
fun toFloat(): Float
Link copied to clipboard
fun toInt(): Int

The low 32 bits, wrapping the way a cast does.

Link copied to clipboard
fun toLong(): Long

The low 64 bits, wrapping the way a cast does.

Link copied to clipboard
fun toShort(): Short
Link copied to clipboard
open override fun toString(): String
fun toString(radix: Int): String
Link copied to clipboard
fun xor(other: KBigInt): KBigInt