Skip to content
Merged

dev #26

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ plugins {
}

group = "io.github.gatrongdev"
version = "0.0.13"
version = "0.0.14"

kotlin {
androidTarget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ actual class KBigDecimalImpl actual constructor(value: String) : KBigDecimal {
return bigDecimal.toString()
}

actual override fun getString(): String {
return bigDecimal.toString()
}
// getString() removed - use toString() instead

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ actual class KBigIntegerImpl actual constructor(value: String) : KBigInteger {
return KBigDecimalImpl(bigInteger.toString())
}

actual override fun getString(): String {
return bigInteger.toString()
}
// getString() removed - use toString() instead

actual override fun add(other: KBigInteger): KBigInteger {
val otherImpl = other as KBigIntegerImpl
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,130 @@
package io.github.gatrongdev.kbignum.math

/**
* Interface for arbitrary-precision decimal arithmetic.
* Provides operations for mathematical calculations with decimal numbers of unlimited precision.
*/
interface KBigDecimal : Comparable<KBigDecimal> {
/**
* Returns a KBigDecimal that is the sum of this and the specified value.
* @param other The value to add to this number
* @return The result of the addition
*/
fun add(other: KBigDecimal): KBigDecimal

/**
* Returns a KBigDecimal that is the difference of this and the specified value.
* @param other The value to subtract from this number
* @return The result of the subtraction
*/
fun subtract(other: KBigDecimal): KBigDecimal

/**
* Returns a KBigDecimal that is the product of this and the specified value.
* @param other The value to multiply with this number
* @return The result of the multiplication
*/
fun multiply(other: KBigDecimal): KBigDecimal

/**
* Returns a KBigDecimal that is the quotient of this divided by the specified value.
* @param other The divisor
* @param scale The number of digits to the right of the decimal point in the result
* @return The result of the division with the specified scale
* @throws ArithmeticException if other is zero
*/
fun divide(
other: KBigDecimal,
scale: Int,
): KBigDecimal

/**
* Returns a KBigDecimal that is the quotient of this divided by the specified value.
* @param other The divisor
* @param scale The number of digits to the right of the decimal point in the result
* @param mode The rounding mode to apply (see RoundingMode constants)
* @return The result of the division with the specified scale and rounding mode
* @throws ArithmeticException if other is zero or if rounding is necessary but the rounding mode is UNNECESSARY
*/
fun divide(
other: KBigDecimal,
scale: Int,
mode: Int,
): KBigDecimal

/**
* Returns the absolute value of this KBigDecimal.
* @return The absolute value (always non-negative)
*/
fun abs(): KBigDecimal

/**
* Returns the signum function of this KBigDecimal.
* @return -1 if negative, 0 if zero, 1 if positive
*/
fun signum(): Int

/**
* Returns a KBigDecimal with the specified scale and rounding mode.
* @param scale The number of digits to the right of the decimal point
* @param roundingMode The rounding mode to apply (see RoundingMode constants)
* @return A KBigDecimal with the specified scale
* @throws ArithmeticException if rounding is necessary but the rounding mode is UNNECESSARY
*/
fun setScale(
scale: Int,
roundingMode: Int,
): KBigDecimal

/**
* Converts this KBigDecimal to a KBigInteger by discarding the fractional part.
* @return KBigInteger representation of this decimal (truncated towards zero)
*/
fun toBigInteger(): KBigInteger

/**
* Returns the string representation of this KBigDecimal.
* @return String representation of the number
*/
override fun toString(): String

fun getString(): String
// getString() removed - use toString() instead

// Additional utility functions

/**
* Returns the negation of this KBigDecimal.
* @return The negative value of this number
*/
fun negate(): KBigDecimal = KBigDecimalFactory.ZERO.subtract(this)

/**
* Returns the scale of this KBigDecimal (number of digits to the right of the decimal point).
* @return The scale of this decimal number
*/
fun scale(): Int = 0 // Default implementation, can be overridden

/**
* Returns the precision of this KBigDecimal (total number of significant digits).
* @return The number of significant digits in this decimal number
*/
fun precision(): Int = toString().replace(".", "").replace("-", "").length

/**
* Checks if this KBigDecimal is equal to zero.
* @return true if this number equals zero, false otherwise
*/
fun isZero(): Boolean = signum() == 0

/**
* Checks if this KBigDecimal is positive (greater than zero).
* @return true if this number is positive, false otherwise
*/
fun isPositive(): Boolean = signum() > 0

/**
* Checks if this KBigDecimal is negative (less than zero).
* @return true if this number is negative, false otherwise
*/
fun isNegative(): Boolean = signum() < 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ expect class KBigDecimalImpl(value: String) : KBigDecimal {

override fun toString(): String

override fun getString(): String
// getString() removed - use toString() instead
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,110 @@
package io.github.gatrongdev.kbignum.math

/**
* Interface for arbitrary-precision integer arithmetic.
* Provides operations for mathematical calculations with integers of unlimited size.
*/
interface KBigInteger {
/**
* Converts this KBigInteger to a Long value.
* @return The Long representation of this number
* @throws ArithmeticException if the value is too large to fit in a Long
*/
fun toLong(): Long

/**
* Returns the string representation of this KBigInteger.
* @return String representation of the number
*/
override fun toString(): String

/**
* Converts this KBigInteger to a KBigDecimal for precise decimal operations.
* @return KBigDecimal representation of this integer
*/
fun toPreciseNumber(): KBigDecimal

fun getString(): String
// getString() removed - use toString() instead

// Additional utility functions

/**
* Returns a KBigInteger that is the sum of this and the specified value.
* @param other The value to add to this number
* @return The result of the addition
*/
fun add(other: KBigInteger): KBigInteger

/**
* Returns a KBigInteger that is the difference of this and the specified value.
* @param other The value to subtract from this number
* @return The result of the subtraction
*/
fun subtract(other: KBigInteger): KBigInteger

/**
* Returns a KBigInteger that is the product of this and the specified value.
* @param other The value to multiply with this number
* @return The result of the multiplication
*/
fun multiply(other: KBigInteger): KBigInteger

/**
* Returns a KBigInteger that is the quotient of this divided by the specified value.
* @param other The divisor
* @return The result of the division (quotient only)
* @throws ArithmeticException if other is zero
*/
fun divide(other: KBigInteger): KBigInteger

/**
* Returns a KBigInteger that is the remainder of this divided by the specified value.
* @param other The divisor
* @return The remainder of the division
* @throws ArithmeticException if other is zero
*/
fun mod(other: KBigInteger): KBigInteger

/**
* Returns the absolute value of this KBigInteger.
* @return The absolute value (always non-negative)
*/
fun abs(): KBigInteger

/**
* Returns the signum function of this KBigInteger.
* @return -1 if negative, 0 if zero, 1 if positive
*/
fun signum(): Int

/**
* Returns the negation of this KBigInteger.
* @return The negative value of this number
*/
fun negate(): KBigInteger = KBigIntegerFactory.ZERO.subtract(this)

fun compareTo(other: KBigInteger): Int

/**
* Compares this KBigInteger with the specified KBigInteger.
* @param other The KBigInteger to compare with
* @return -1 if this < other, 0 if this == other, 1 if this > other
*/
operator fun compareTo(other: KBigInteger): Int

/**
* Checks if this KBigInteger is equal to zero.
* @return true if this number equals zero, false otherwise
*/
fun isZero(): Boolean = toString() == "0"

/**
* Checks if this KBigInteger is positive (greater than zero).
* @return true if this number is positive, false otherwise
*/
fun isPositive(): Boolean = signum() > 0

/**
* Checks if this KBigInteger is negative (less than zero).
* @return true if this number is negative, false otherwise
*/
fun isNegative(): Boolean = signum() < 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ expect class KBigIntegerImpl(value: String) : KBigInteger {

override fun toPreciseNumber(): KBigDecimal

override fun getString(): String
// getString() removed - use toString() instead

override fun add(other: KBigInteger): KBigInteger

Expand Down
Loading