diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts index 69e0c2f..f783545 100644 --- a/shared/build.gradle.kts +++ b/shared/build.gradle.kts @@ -17,7 +17,7 @@ plugins { } group = "io.github.gatrongdev" -version = "0.0.13" +version = "0.0.14" kotlin { androidTarget { diff --git a/shared/src/androidMain/kotlin/io/github/gatrongdev/kbignum/math/KBigDecimalImpl.android.kt b/shared/src/androidMain/kotlin/io/github/gatrongdev/kbignum/math/KBigDecimalImpl.android.kt index eb289b5..187d470 100644 --- a/shared/src/androidMain/kotlin/io/github/gatrongdev/kbignum/math/KBigDecimalImpl.android.kt +++ b/shared/src/androidMain/kotlin/io/github/gatrongdev/kbignum/math/KBigDecimalImpl.android.kt @@ -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 diff --git a/shared/src/androidMain/kotlin/io/github/gatrongdev/kbignum/math/KBigIntegerImpl.android.kt b/shared/src/androidMain/kotlin/io/github/gatrongdev/kbignum/math/KBigIntegerImpl.android.kt index a1d0f21..49a1c01 100644 --- a/shared/src/androidMain/kotlin/io/github/gatrongdev/kbignum/math/KBigIntegerImpl.android.kt +++ b/shared/src/androidMain/kotlin/io/github/gatrongdev/kbignum/math/KBigIntegerImpl.android.kt @@ -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 diff --git a/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigDecimal.kt b/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigDecimal.kt index 2a7d10b..908dba3 100644 --- a/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigDecimal.kt +++ b/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigDecimal.kt @@ -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 { + /** + * 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 } diff --git a/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigDecimalImpl.kt b/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigDecimalImpl.kt index 55e0154..863a9cc 100644 --- a/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigDecimalImpl.kt +++ b/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigDecimalImpl.kt @@ -43,5 +43,5 @@ expect class KBigDecimalImpl(value: String) : KBigDecimal { override fun toString(): String - override fun getString(): String + // getString() removed - use toString() instead } diff --git a/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigInteger.kt b/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigInteger.kt index fefbf93..9d4a97f 100644 --- a/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigInteger.kt +++ b/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigInteger.kt @@ -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 } diff --git a/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigIntegerImpl.kt b/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigIntegerImpl.kt index cfd2eb1..a73a418 100644 --- a/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigIntegerImpl.kt +++ b/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigIntegerImpl.kt @@ -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 diff --git a/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigMath.kt b/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigMath.kt index 364a206..47c1a0b 100644 --- a/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigMath.kt +++ b/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigMath.kt @@ -1,11 +1,16 @@ package io.github.gatrongdev.kbignum.math /** - * Mathematical utility functions for KBigDecimal and KBigInteger + * Mathematical utility functions for KBigDecimal and KBigInteger. + * Provides advanced mathematical operations not available in the basic interfaces. */ object KBigMath { /** - * Calculate square root of a KBigDecimal using Newton's method + * Calculates the square root of a KBigDecimal using Newton's method for iterative approximation. + * @param value The KBigDecimal to calculate the square root of + * @param scale The number of decimal places in the result (default: 10) + * @return The square root of the input value with the specified precision + * @throws IllegalArgumentException if the value is negative */ fun sqrt( value: KBigDecimal, @@ -32,7 +37,11 @@ object KBigMath { } /** - * Calculate factorial of a KBigInteger + * Calculates the factorial of a KBigInteger (n!). + * The factorial of n is the product of all positive integers less than or equal to n. + * @param n The KBigInteger to calculate the factorial of + * @return The factorial of n (n!) + * @throws IllegalArgumentException if n is negative */ fun factorial(n: KBigInteger): KBigInteger { if (n.signum() < 0) { @@ -52,7 +61,11 @@ object KBigMath { } /** - * Calculate greatest common divisor (GCD) of two KBigIntegers + * Calculates the greatest common divisor (GCD) of two KBigIntegers using Euclid's algorithm. + * The GCD is the largest positive integer that divides both numbers without remainder. + * @param a The first KBigInteger + * @param b The second KBigInteger + * @return The greatest common divisor of a and b */ fun gcd( a: KBigInteger, @@ -71,7 +84,11 @@ object KBigMath { } /** - * Calculate least common multiple (LCM) of two KBigIntegers + * Calculates the least common multiple (LCM) of two KBigIntegers. + * The LCM is the smallest positive integer that is divisible by both numbers. + * @param a The first KBigInteger + * @param b The second KBigInteger + * @return The least common multiple of a and b, or zero if either input is zero */ fun lcm( a: KBigInteger, @@ -85,7 +102,10 @@ object KBigMath { } /** - * Check if a KBigInteger is prime (basic implementation) + * Checks if a KBigInteger is a prime number using trial division. + * A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. + * @param n The KBigInteger to test for primality + * @return true if n is prime, false otherwise */ fun isPrime(n: KBigInteger): Boolean { if (n.compareTo(KBigIntegerFactory.fromInt(2)) < 0) { @@ -114,7 +134,12 @@ object KBigMath { } /** - * Calculate power with KBigInteger base and exponent + * Calculates the power of a KBigInteger raised to another KBigInteger exponent using binary exponentiation. + * This method efficiently computes base^exponent. + * @param base The base KBigInteger + * @param exponent The exponent KBigInteger (must be non-negative) + * @return The result of base raised to the power of exponent + * @throws IllegalArgumentException if the exponent is negative */ fun pow( base: KBigInteger, @@ -143,36 +168,6 @@ object KBigMath { return result } - /** - * Calculate absolute value - */ - fun abs(value: KBigDecimal): KBigDecimal = value.abs() - - fun abs(value: KBigInteger): KBigInteger = value.abs() - - /** - * Calculate maximum of two values - */ - fun max( - a: KBigDecimal, - b: KBigDecimal, - ): KBigDecimal = a.max(b) - - fun max( - a: KBigInteger, - b: KBigInteger, - ): KBigInteger = a.max(b) - - /** - * Calculate minimum of two values - */ - fun min( - a: KBigDecimal, - b: KBigDecimal, - ): KBigDecimal = a.min(b) - - fun min( - a: KBigInteger, - b: KBigInteger, - ): KBigInteger = a.min(b) + // abs(), max(), min() functions are available as methods on the objects themselves + // Use value.abs(), a.max(b), a.min(b) instead of KBigMath.abs(value), KBigMath.max(a, b), KBigMath.min(a, b) } diff --git a/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigNumberExtensions.kt b/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigNumberExtensions.kt index c83b3df..97b4500 100644 --- a/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigNumberExtensions.kt +++ b/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigNumberExtensions.kt @@ -1,58 +1,185 @@ package io.github.gatrongdev.kbignum.math // Extension functions for converting common types to KBigDecimal + +/** + * Converts a String to a KBigDecimal. + * @receiver The String representation of a decimal number + * @return A KBigDecimal representing the value of this string + * @throws NumberFormatException if the string is not a valid decimal representation + */ fun String.toKBigDecimal(): KBigDecimal = KBigDecimalFactory.fromString(this) +/** + * Converts an Int to a KBigDecimal. + * @receiver The Int value to convert + * @return A KBigDecimal representing this integer value + */ fun Int.toKBigDecimal(): KBigDecimal = KBigDecimalFactory.fromInt(this) +/** + * Converts a Long to a KBigDecimal. + * @receiver The Long value to convert + * @return A KBigDecimal representing this long value + */ fun Long.toKBigDecimal(): KBigDecimal = KBigDecimalFactory.fromLong(this) -fun Double.toKBigDecimal(): KBigDecimal = KBigDecimalFactory.fromDouble(this) +/** + * Converts a Double to a KBigDecimal. + * @receiver The Double value to convert + * @return A KBigDecimal representing this double value + */ +fun Double.toKBigDecimal(): KBigDecimal = KBigDecimalImpl(this.toString()) -fun Float.toKBigDecimal(): KBigDecimal = KBigDecimalFactory.fromFloat(this) +/** + * Converts a Float to a KBigDecimal. + * @receiver The Float value to convert + * @return A KBigDecimal representing this float value + */ +fun Float.toKBigDecimal(): KBigDecimal = KBigDecimalImpl(this.toString()) // Extension functions for converting common types to KBigInteger + +/** + * Converts a String to a KBigInteger. + * @receiver The String representation of an integer + * @return A KBigInteger representing the value of this string + * @throws NumberFormatException if the string is not a valid integer representation + */ fun String.toKBigInteger(): KBigInteger = KBigIntegerFactory.fromString(this) +/** + * Converts an Int to a KBigInteger. + * @receiver The Int value to convert + * @return A KBigInteger representing this integer value + */ fun Int.toKBigInteger(): KBigInteger = KBigIntegerFactory.fromInt(this) +/** + * Converts a Long to a KBigInteger. + * @receiver The Long value to convert + * @return A KBigInteger representing this long value + */ fun Long.toKBigInteger(): KBigInteger = KBigIntegerFactory.fromLong(this) // Operator overloads for KBigDecimal + +/** + * Addition operator for KBigDecimal. + * @receiver The first operand + * @param other The second operand to add + * @return The sum of this and other + */ operator fun KBigDecimal.plus(other: KBigDecimal): KBigDecimal = this.add(other) +/** + * Subtraction operator for KBigDecimal. + * @receiver The minuend + * @param other The subtrahend to subtract + * @return The difference of this minus other + */ operator fun KBigDecimal.minus(other: KBigDecimal): KBigDecimal = this.subtract(other) +/** + * Multiplication operator for KBigDecimal. + * @receiver The first factor + * @param other The second factor to multiply + * @return The product of this and other + */ operator fun KBigDecimal.times(other: KBigDecimal): KBigDecimal = this.multiply(other) +/** + * Division operator for KBigDecimal with default scale and rounding. + * @receiver The dividend + * @param other The divisor + * @return The quotient with scale 10 and HALF_UP rounding + * @throws ArithmeticException if other is zero + */ operator fun KBigDecimal.div(other: KBigDecimal): KBigDecimal = this.divide(other, 10, 4) +/** + * Unary minus operator for KBigDecimal. + * @receiver The KBigDecimal to negate + * @return The negation of this number + */ operator fun KBigDecimal.unaryMinus(): KBigDecimal = KBigDecimalFactory.ZERO.subtract(this) +/** + * Unary plus operator for KBigDecimal (identity operation). + * @receiver The KBigDecimal + * @return This number unchanged + */ operator fun KBigDecimal.unaryPlus(): KBigDecimal = this // Operator overloads for KBigInteger + +/** + * Addition operator for KBigInteger. + * @receiver The first operand + * @param other The second operand to add + * @return The sum of this and other + */ operator fun KBigInteger.plus(other: KBigInteger): KBigInteger = this.add(other) +/** + * Subtraction operator for KBigInteger. + * @receiver The minuend + * @param other The subtrahend to subtract + * @return The difference of this minus other + */ operator fun KBigInteger.minus(other: KBigInteger): KBigInteger = this.subtract(other) +/** + * Multiplication operator for KBigInteger. + * @receiver The first factor + * @param other The second factor to multiply + * @return The product of this and other + */ operator fun KBigInteger.times(other: KBigInteger): KBigInteger = this.multiply(other) +/** + * Division operator for KBigInteger. + * @receiver The dividend + * @param other The divisor + * @return The quotient of this divided by other + * @throws ArithmeticException if other is zero + */ operator fun KBigInteger.div(other: KBigInteger): KBigInteger = this.divide(other) +/** + * Remainder operator for KBigInteger. + * @receiver The dividend + * @param other The divisor + * @return The remainder of this divided by other + * @throws ArithmeticException if other is zero + */ operator fun KBigInteger.rem(other: KBigInteger): KBigInteger = this.mod(other) +/** + * Unary minus operator for KBigInteger. + * @receiver The KBigInteger to negate + * @return The negation of this number + */ operator fun KBigInteger.unaryMinus(): KBigInteger = KBigIntegerFactory.ZERO.subtract(this) +/** + * Unary plus operator for KBigInteger (identity operation). + * @receiver The KBigInteger + * @return This number unchanged + */ operator fun KBigInteger.unaryPlus(): KBigInteger = this -// Comparison operators for KBigDecimal -operator fun KBigDecimal.compareTo(other: KBigDecimal): Int = this.compareTo(other) - -// Comparison operators for KBigInteger -operator fun KBigInteger.compareTo(other: KBigInteger): Int = this.compareTo(other) +// Comparison operators are inherited from Comparable interface and implemented in the interfaces // Math functions for KBigDecimal + +/** + * Calculates this KBigDecimal raised to the power of the specified integer exponent. + * @receiver The base KBigDecimal + * @param exponent The integer exponent (must be non-negative) + * @return This KBigDecimal raised to the power of exponent + * @throws IllegalArgumentException if the exponent is negative + */ fun KBigDecimal.pow(exponent: Int): KBigDecimal { var result = KBigDecimalFactory.ONE var base = this @@ -74,6 +201,14 @@ fun KBigDecimal.pow(exponent: Int): KBigDecimal { } // Math functions for KBigInteger + +/** + * Calculates this KBigInteger raised to the power of the specified integer exponent. + * @receiver The base KBigInteger + * @param exponent The integer exponent (must be non-negative) + * @return This KBigInteger raised to the power of exponent + * @throws IllegalArgumentException if the exponent is negative + */ fun KBigInteger.pow(exponent: Int): KBigInteger { var result = KBigIntegerFactory.ONE var base = this @@ -94,22 +229,36 @@ fun KBigInteger.pow(exponent: Int): KBigInteger { return result } -fun KBigDecimal.isZero(): Boolean = this.compareTo(KBigDecimalFactory.ZERO) == 0 - -fun KBigDecimal.isPositive(): Boolean = this.signum() > 0 - -fun KBigDecimal.isNegative(): Boolean = this.signum() < 0 - -fun KBigInteger.isZero(): Boolean = this.compareTo(KBigIntegerFactory.ZERO) == 0 - -fun KBigInteger.isPositive(): Boolean = this.signum() > 0 - -fun KBigInteger.isNegative(): Boolean = this.signum() < 0 +// isZero(), isPositive(), isNegative() functions are already implemented in the interfaces +/** + * Returns the maximum of this KBigDecimal and the specified value. + * @receiver The first KBigDecimal to compare + * @param other The second KBigDecimal to compare + * @return The larger of the two values + */ fun KBigDecimal.max(other: KBigDecimal): KBigDecimal = if (this >= other) this else other +/** + * Returns the minimum of this KBigDecimal and the specified value. + * @receiver The first KBigDecimal to compare + * @param other The second KBigDecimal to compare + * @return The smaller of the two values + */ fun KBigDecimal.min(other: KBigDecimal): KBigDecimal = if (this <= other) this else other +/** + * Returns the maximum of this KBigInteger and the specified value. + * @receiver The first KBigInteger to compare + * @param other The second KBigInteger to compare + * @return The larger of the two values + */ fun KBigInteger.max(other: KBigInteger): KBigInteger = if (this >= other) this else other +/** + * Returns the minimum of this KBigInteger and the specified value. + * @receiver The first KBigInteger to compare + * @param other The second KBigInteger to compare + * @return The smaller of the two values + */ fun KBigInteger.min(other: KBigInteger): KBigInteger = if (this <= other) this else other diff --git a/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigNumberFactory.kt b/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigNumberFactory.kt index 054d951..9fa45b3 100644 --- a/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigNumberFactory.kt +++ b/shared/src/commonMain/kotlin/io/github/gatrongdev/kbignum/math/KBigNumberFactory.kt @@ -1,65 +1,119 @@ package io.github.gatrongdev.kbignum.math /** - * Factory object for creating KBigDecimal instances + * Factory object for creating KBigDecimal instances. + * Provides convenient methods for creating KBigDecimal objects from various types. */ object KBigDecimalFactory { + /** Constant representing zero as a KBigDecimal */ val ZERO: KBigDecimal = KBigDecimalImpl.ZERO + + /** Constant representing one as a KBigDecimal */ val ONE: KBigDecimal = KBigDecimalImpl("1") + + /** Constant representing ten as a KBigDecimal */ val TEN: KBigDecimal = KBigDecimalImpl("10") + /** + * Creates a KBigDecimal from a string representation. + * @param value The string representation of the decimal number + * @return A KBigDecimal representing the specified value + * @throws NumberFormatException if the string is not a valid decimal representation + */ fun fromString(value: String): KBigDecimal { return KBigDecimalImpl(value) } + /** + * Creates a KBigDecimal from a Long value. + * @param value The Long value to convert + * @return A KBigDecimal representing the specified Long value + */ fun fromLong(value: Long): KBigDecimal { return KBigDecimalImpl.fromLong(value) } + /** + * Creates a KBigDecimal from an Int value. + * @param value The Int value to convert + * @return A KBigDecimal representing the specified Int value + */ fun fromInt(value: Int): KBigDecimal { return KBigDecimalImpl.fromInt(value) } - fun fromDouble(value: Double): KBigDecimal { - return KBigDecimalImpl(value.toString()) - } - - fun fromFloat(value: Float): KBigDecimal { - return KBigDecimalImpl(value.toString()) - } + // fromDouble and fromFloat removed - use extension functions Double.toKBigDecimal() and Float.toKBigDecimal() instead } /** - * Factory object for creating KBigInteger instances + * Factory object for creating KBigInteger instances. + * Provides convenient methods for creating KBigInteger objects from various types. */ object KBigIntegerFactory { + /** Constant representing zero as a KBigInteger */ val ZERO: KBigInteger = KBigIntegerImpl.ZERO + + /** Constant representing one as a KBigInteger */ val ONE: KBigInteger = KBigIntegerImpl("1") + + /** Constant representing ten as a KBigInteger */ val TEN: KBigInteger = KBigIntegerImpl("10") + /** + * Creates a KBigInteger from a string representation. + * @param value The string representation of the integer + * @return A KBigInteger representing the specified value + * @throws NumberFormatException if the string is not a valid integer representation + */ fun fromString(value: String): KBigInteger { return KBigIntegerImpl(value) } + /** + * Creates a KBigInteger from a Long value. + * @param value The Long value to convert + * @return A KBigInteger representing the specified Long value + */ fun fromLong(value: Long): KBigInteger { return KBigIntegerImpl.fromLong(value) } + /** + * Creates a KBigInteger from an Int value. + * @param value The Int value to convert + * @return A KBigInteger representing the specified Int value + */ fun fromInt(value: Int): KBigInteger { return KBigIntegerImpl.fromInt(value) } } /** - * Rounding mode constants + * Rounding mode constants for KBigDecimal operations. + * These constants define how rounding should be performed when precision must be reduced. */ object RoundingMode { + /** Rounding mode to round away from zero */ const val UP = 0 + + /** Rounding mode to round towards zero */ const val DOWN = 1 + + /** Rounding mode to round towards positive infinity */ const val CEILING = 2 + + /** Rounding mode to round towards negative infinity */ const val FLOOR = 3 + + /** Rounding mode to round towards the nearest neighbor, with ties rounded up */ const val HALF_UP = 4 + + /** Rounding mode to round towards the nearest neighbor, with ties rounded down */ const val HALF_DOWN = 5 + + /** Rounding mode to round towards the nearest neighbor, with ties rounded to the even neighbor */ const val HALF_EVEN = 6 + + /** Rounding mode to assert that the requested operation has an exact result */ const val UNNECESSARY = 7 } diff --git a/shared/src/commonTest/kotlin/io/github/gatrongdev/kbignum/math/KBigIntegerTest.kt b/shared/src/commonTest/kotlin/io/github/gatrongdev/kbignum/math/KBigIntegerTest.kt index 015afca..53268de 100644 --- a/shared/src/commonTest/kotlin/io/github/gatrongdev/kbignum/math/KBigIntegerTest.kt +++ b/shared/src/commonTest/kotlin/io/github/gatrongdev/kbignum/math/KBigIntegerTest.kt @@ -45,7 +45,6 @@ class KBigIntegerTest { @Test fun testStringValue() { val bigInt = "999999999999999999".toKBigInteger() - assertEquals("999999999999999999", bigInt.getString()) assertEquals("999999999999999999", bigInt.toString()) } } diff --git a/shared/src/iosMain/kotlin/io/github/gatrongdev/kbignum/math/KBigDecimalImpl.ios.kt b/shared/src/iosMain/kotlin/io/github/gatrongdev/kbignum/math/KBigDecimalImpl.ios.kt index 2e27afb..7fdc659 100644 --- a/shared/src/iosMain/kotlin/io/github/gatrongdev/kbignum/math/KBigDecimalImpl.ios.kt +++ b/shared/src/iosMain/kotlin/io/github/gatrongdev/kbignum/math/KBigDecimalImpl.ios.kt @@ -124,9 +124,7 @@ actual class KBigDecimalImpl actual constructor(value: String) : KBigDecimal { return nsDecimalNumber.stringValue } - actual override fun getString(): String { - return nsDecimalNumber.stringValue - } + // getString() removed - use toString() instead override fun equals(other: Any?): Boolean { if (this === other) return true diff --git a/shared/src/iosMain/kotlin/io/github/gatrongdev/kbignum/math/KBigIntegerImpl.ios.kt b/shared/src/iosMain/kotlin/io/github/gatrongdev/kbignum/math/KBigIntegerImpl.ios.kt index 63c6ebc..4caadc1 100644 --- a/shared/src/iosMain/kotlin/io/github/gatrongdev/kbignum/math/KBigIntegerImpl.ios.kt +++ b/shared/src/iosMain/kotlin/io/github/gatrongdev/kbignum/math/KBigIntegerImpl.ios.kt @@ -34,9 +34,7 @@ actual class KBigIntegerImpl actual constructor(value: String) : KBigInteger { return KBigDecimalImpl(nsNumber.stringValue) } - actual override fun getString(): String { - return nsNumber.stringValue - } + // getString() removed - use toString() instead actual override fun add(other: KBigInteger): KBigInteger { val otherImpl = other as KBigIntegerImpl