kotest icon indicating copy to clipboard operation
kotest copied to clipboard

allow equals comparison of BigDecimals

Open hughlunnon opened this issue 2 years ago • 6 comments

currently you can't easily compare bigdecimals for equality in kotest (ie : BigDecimal("1.0") shouldBe BigDecimal("1") passing)

if we add the following

infix fun BigDecimal?.shouldEqual(other: BigDecimal?) = this?.compareTo(other) shouldBe 0

to the BigDecimal comparitors file, it'll make it easier

happy to raise a PR

hughlunnon avatar Dec 13 '23 22:12 hughlunnon

We could also add an Eq instance for BigDecimal to make it work? then BigDecimal("1.0") shouldBe BigDecimal("1") would work which I think is more intuitive ?

sksamuel avatar Dec 14 '23 07:12 sksamuel

it is more intuitive but those two bigdecimals aren't equal according to the bigdecimal spec (BigDecimal("1.0")==BigDecimal("1") is false) due to the number of decimal spots. Changing "shouldBe" to return true in that circumstance might be undesirable for some folks?

hughlunnon avatar Dec 15 '23 13:12 hughlunnon

BigDecimal would have the folloring assertions then, given this suggestion

  • shouldBe
  • shouldEqual
  • shouldBeEqual

I think it might be a bit confusing to know which to pick. Perhaps naming the new one shouldHaveSameValue would set it apart sufficiently?

Kantis avatar Dec 15 '23 22:12 Kantis

We do have matchers for compareTo

  • shouldBeEqualComparingTo

Would this solve the issue for different scales? BigDecimal returns 0 for BigDecimal("1.0").compareTo(BigDecimal("1")

So one could just use

BigDecimal("1.005") shouldBeEqualComparingTo BigDecimal("1.0050000")

LeoColman avatar Jan 01 '24 16:01 LeoColman

We do have matchers for compareTo

  • shouldBeEqualComparingTo

Would this solve the issue for different scales? BigDecimal returns 0 for BigDecimal("1.0").compareTo(BigDecimal("1")

So one could just use

BigDecimal("1.005") shouldBeEqualComparingTo BigDecimal("1.0050000")

ah yes that does work, but I think (given a few of us weren't aware of it) - its not very discoverable. A function alias within the io.kotest.matchers.bigdecimal package would be nice to aid discovery?

hughlunnon avatar Jan 02 '24 18:01 hughlunnon

I think so, yes!

Now what to call it is tough. Any idea?

LeoColman avatar Jan 10 '24 19:01 LeoColman

shouldBeEqualWithoutScale? as a match in the bigdecimal package ?

sksamuel avatar Mar 10 '24 02:03 sksamuel

shouldBeSameValue?

LeoColman avatar Mar 10 '24 03:03 LeoColman

Value is a word that could be used either way. Is 5 and 5.0 the same value or not the same value in general?

sksamuel avatar Mar 10 '24 03:03 sksamuel

@LeoColman what's your final thoughts before I implement it?

sksamuel avatar Mar 10 '24 21:03 sksamuel

I'm trying to think if mathematically we have a term that means 'this number represents the same value as this one, in totality'

And I can only think of shouldBeNumericallyEquivalentTo or shouldBeMathematicallyEquivalentTo

LeoColman avatar Mar 10 '24 23:03 LeoColman

So 15.05 shouldBeNumericallyEquivalentTo 15.050000

LeoColman avatar Mar 10 '24 23:03 LeoColman

How come you don't like the word scale? Since that's what big decimal itself uses. https://medium.com/beingabetterdeveloper/comparing-bigdecimal-s-with-different-scales-2901bf26538f

sksamuel avatar Mar 11 '24 00:03 sksamuel

Like shouldBeIgnoringScale or the one you suggested?

Nothing against it at all 😁 go for it

LeoColman avatar Mar 11 '24 00:03 LeoColman

shouldBeIgnoringScale or shouldBeEqualIgnoringScale What do you think ?

sksamuel avatar Mar 11 '24 00:03 sksamuel

shouldBeEqualIgnoringScale sounds better

LeoColman avatar Mar 11 '24 00:03 LeoColman

@LeoColman https://github.com/kotest/kotest/pull/3924

sksamuel avatar Mar 11 '24 00:03 sksamuel