ionspin / kotlin-multiplatform-bignum

A Kotlin multiplatform library for arbitrary precision arithmetics
Apache License 2.0
350 stars 41 forks source link

BigDecimal .toBigDecimal(...) extension functions have confusing behavior #164

Closed ionspin closed 3 years ago

ionspin commented 3 years ago

Describe the bug BigDecimal .toBigDecimal(...) extension functions behave differently if the exponent is supplied or not. If it is, library calls the BigDecimal.fromIntWithExponent(...) method which considers the integer input as a significand. On the other hand if it isn't then the library calls BigDecimal.fromInt(...) method, which considers the integer input as the whole number.

i.e.

val oneHundred = 100.toBigDecimal()
val one = 100.toBigDecimal(exponent = 0)     // 1.00   100 becomes significand with exponent 0 or 1.00E0
val another = 1234..toBigDecimal(exponent = 2) // 12.34   1234 becomes significand with exponent 2 or 1.234E2

The original motivation was to provide a way to easily define a big value like 5.toBigDecimal(exponent = 100) but it was clumsily included in the extension function. Expected behavior API provides both behaviours in a clear and concise manner