This commit is contained in:
DBotThePony 2023-03-13 10:40:38 +07:00
parent 0b64ffafaa
commit 2243b65cec
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 4 additions and 10 deletions

View File

@ -558,14 +558,8 @@ class Decimal @JvmOverloads constructor(whole: BigInteger, decimal: Double = 0.0
} }
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (isNaN) if (isNaN) return false
return false return other === this || other is Decimal && other.whole == whole && weakEqualDoubles(decimal, other.decimal)
if (other is Decimal) {
return other.whole == whole && decimal == other.decimal
}
return super.equals(other)
} }
override fun hashCode(): Int { override fun hashCode(): Int {
@ -632,7 +626,7 @@ class Decimal @JvmOverloads constructor(whole: BigInteger, decimal: Double = 0.0
return 1 return 1
var cmp = whole.compareTo(other.whole) var cmp = whole.compareTo(other.whole)
if (cmp == 0) cmp = decimal.compareTo(other.decimal) if (cmp == 0) cmp = weakCompareDoubles(decimal, other.decimal)
return cmp return cmp
} }

View File

@ -30,7 +30,7 @@ object DecimalTests {
check((Decimal(0, 0.5) + Decimal(0, 0.5)) == Decimal(1)) { "0.5 + 0.5 != 1" } check((Decimal(0, 0.5) + Decimal(0, 0.5)) == Decimal(1)) { "0.5 + 0.5 != 1" }
check((Decimal(0, 0.5) + Decimal(0, -0.5)) == Decimal(0)) { "0.5 + -0.5 != 1" } check((Decimal(0, 0.5) + Decimal(0, -0.5)) == Decimal(0)) { "0.5 + -0.5 != 1" }
check((Decimal(0, 0.5) - Decimal(0, -0.5)) == Decimal(1)) { "0.5 - -0.5 != 1" } check((Decimal(0, 0.5) - Decimal(0, -0.5)) == Decimal(1)) { "0.5 - -0.5 != 1" }
check((Decimal(0, 0.3) - Decimal(1, 0.2)) == Decimal(0, -0.9)) { "0.3 - 1.2 != -0.9" } check((Decimal(0, 0.3) - Decimal(1, 0.2)) == Decimal(0, -0.9)) { "0.3 - 1.2 != -0.9 ${Decimal(0, -0.9)} ${Decimal(0, 0.3) - Decimal(1, 0.2)}" }
check((Decimal(0, 0.3) * Decimal(0, 0.3)) == Decimal(0, 0.09)) { "0.3 * 0.3 != 0.9 ${Decimal(0, 0.3) * Decimal(0, 0.3)}" } check((Decimal(0, 0.3) * Decimal(0, 0.3)) == Decimal(0, 0.09)) { "0.3 * 0.3 != 0.9 ${Decimal(0, 0.3) * Decimal(0, 0.3)}" }
check((Decimal(2, 0.3) * Decimal(2, 0.3)) == Decimal(5, 0.29)) { "2.3 * 2.3 != 5.29 ${Decimal(2, 0.3) * Decimal(2, 0.3)}" } check((Decimal(2, 0.3) * Decimal(2, 0.3)) == Decimal(5, 0.29)) { "2.3 * 2.3 != 5.29 ${Decimal(2, 0.3) * Decimal(2, 0.3)}" }
check((Decimal(4) / Decimal(2)) == Decimal(2)) { "4 / 2 != 2" } check((Decimal(4) / Decimal(2)) == Decimal(2)) { "4 / 2 != 2" }