Skip to content

Commit

Permalink
Fix long narrowing loosing sign, remove signum from bigdecimal toDoub…
Browse files Browse the repository at this point in the history
…le because it was now being applied twice. Add some tests, update changelog.
  • Loading branch information
ionspin committed Aug 20, 2021
1 parent 3b4ebca commit 4daa4f6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
- Added kotlinx serialization support library
- Enabled gradle dependencies verification (bootstrapped)
- Fix for losing decimal mode when using unary minus (#184)
- Fix for losing sign when narrowing to long from big integer (#186)


##### 0.3.1
##### 0.3.1 - 10.5.2021
- Fix for #176, a case of unclear API. Methods `roundToDigitPositionAfterDecimalPoint` and `roundToDigitPosition` would set decimal precision to the number of digits present in the result after the rounding was completed. Now they only set decimal precision if it's explicitly set, otherwise it stays unlimited.
- Bump to 1.5.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1877,7 +1877,7 @@ class BigDecimal private constructor(
val divExponent = precision - 1 - exponent
val l = this.significand.longValue(exactRequired)
return if (l.toDouble().toLong() == l && divExponent >= 0 && divExponent < double10pow.size) {
(l / double10pow[divExponent.toInt()]) * signum()
(l / double10pow[divExponent.toInt()])
} else {
toString().toDouble()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ class BigInteger internal constructor(wordArray: WordArray, requestedSign: Sign)
val firstBit = magnitude[1] shl 63
(magnitude[0].toLong() or firstBit.toLong()) * signum()
} else {
return magnitude[0].toLong()
return magnitude[0].toLong() * signum()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,20 +252,38 @@ BigIntegerTest {
val a = 2.toBigInteger()
a.intValue(exactRequired = true) == 2
}
assertTrue {
val a = (-2).toBigInteger()
a.intValue(exactRequired = true) == -2
}
assertTrue {
val a = 2.toBigInteger()
val short: Short = 2
a.shortValue(exactRequired = true) == short
}
assertTrue {
val a = (-2).toBigInteger()
val short: Short = -2
a.shortValue(exactRequired = true) == short
}
assertTrue {
val a = 2.toBigInteger()
val byte: Byte = 2
a.byteValue(exactRequired = true) == byte
}
assertTrue {
val a = (-2).toBigInteger()
val byte: Byte = -2
a.byteValue(exactRequired = true) == byte
}
assertTrue {
val a = 2.toBigInteger()
a.longValue(exactRequired = true) == 2L
}
assertTrue {
val a = (-2).toBigInteger()
a.longValue(exactRequired = true) == -2L
}
assertTrue {
val a = 2.toBigInteger()
a.uintValue(exactRequired = true) == 2U
Expand Down

0 comments on commit 4daa4f6

Please sign in to comment.