-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Kotlin/Native implementation of BigDecimal and BigInteger #1200
Open
ianbotsf
wants to merge
6
commits into
kn-main
Choose a base branch
from
kn-bignums
base: kn-main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
66c7e83
WIP: partial implementation of bignums, pushed for multi-dev debugging
ianbotsf ca1c1f1
working implementation of BigInteger/BigDecimal for Kotlin/Native
ianbotsf 1679172
remove unused import
ianbotsf 5698dc3
remove unnecessary libbacktrace usage
ianbotsf 5d492c3
update API manifest
ianbotsf 16c85d1
tickle CI
ianbotsf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,33 +4,52 @@ | |
*/ | ||
package aws.smithy.kotlin.runtime.content | ||
|
||
public actual class BigDecimal actual constructor(public val value: String) : | ||
import java.math.BigDecimal as JvmBigDecimal | ||
|
||
public actual class BigDecimal private constructor(private val delegate: JvmBigDecimal) : | ||
Number(), | ||
Comparable<BigDecimal> { | ||
private val delegate = java.math.BigDecimal(value) | ||
|
||
public actual constructor(mantissa: BigInteger, exponent: Int) : this( | ||
java.math.BigDecimal( | ||
java.math.BigInteger(mantissa.toString()), | ||
exponent, | ||
).toPlainString(), | ||
) | ||
private companion object { | ||
/** | ||
* Returns a new or existing [BigDecimal] wrapper for the given delegate [value] | ||
* @param value The delegate value to wrap | ||
* @param left A candidate wrapper which may already contain [value] | ||
* @param right A candidate wrapper which may already contain [value] | ||
*/ | ||
fun coalesceOrCreate(value: JvmBigDecimal, left: BigDecimal, right: BigDecimal): BigDecimal = when (value) { | ||
left.delegate -> left | ||
right.delegate -> right | ||
else -> BigDecimal(value) | ||
} | ||
} | ||
|
||
public actual constructor(value: String) : this(JvmBigDecimal(value)) | ||
public actual constructor(mantissa: BigInteger, exponent: Int) : this(JvmBigDecimal(mantissa.delegate, exponent)) | ||
|
||
public actual fun toPlainString(): String = delegate.toPlainString() | ||
actual override fun toByte(): Byte = delegate.toByte() | ||
actual override fun toDouble(): Double = delegate.toDouble() | ||
actual override fun toFloat(): Float = delegate.toFloat() | ||
actual override fun toInt(): Int = delegate.toInt() | ||
actual override fun toLong(): Long = delegate.toLong() | ||
actual override fun toShort(): Short = delegate.toShort() | ||
public actual override fun toString(): String = delegate.toString() | ||
public actual override fun toByte(): Byte = delegate.toByte() | ||
public actual override fun toDouble(): Double = delegate.toDouble() | ||
public actual override fun toFloat(): Float = delegate.toFloat() | ||
public actual override fun toInt(): Int = delegate.toInt() | ||
public actual override fun toLong(): Long = delegate.toLong() | ||
public actual override fun toShort(): Short = delegate.toShort() | ||
|
||
actual override fun equals(other: Any?): Boolean = other is BigDecimal && delegate == other.delegate | ||
public actual override fun equals(other: Any?): Boolean = other is BigDecimal && delegate == other.delegate | ||
public actual override fun hashCode(): Int = 31 + delegate.hashCode() | ||
|
||
public actual val mantissa: BigInteger | ||
get() = BigInteger(delegate.unscaledValue().toString()) | ||
get() = BigInteger(delegate.unscaledValue()) | ||
|
||
public actual val exponent: Int | ||
get() = delegate.scale() | ||
|
||
public actual operator fun plus(other: BigDecimal): BigDecimal = | ||
coalesceOrCreate(delegate + other.delegate, this, other) | ||
|
||
public actual operator fun minus(other: BigDecimal): BigDecimal = | ||
coalesceOrCreate(delegate - other.delegate, this, other) | ||
Comment on lines
+48
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice 👍 |
||
|
||
actual override fun compareTo(other: BigDecimal): Int = delegate.compareTo(other.delegate) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually 31 is multiplied in these hashCode implementations, is this intentional addition? Is there any reason we can't just take the delegate's hash code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I intentionally used addition instead of multiplication although either should work. I wanted to avoid using the unmodified hash code from the delegate because that would bin them the same if they were both added to a hashed container (e.g.,
Set
). That's unlikely but also trivial to avoid.