diff --git a/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala b/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala index efecdb7071..a2085e2cbf 100644 --- a/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala +++ b/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala @@ -314,9 +314,6 @@ class Tests extends TestsConfig with AnyFunSuiteLike with FunSuiteDiscipline wit checkAll("Hash[Queue[Int]", HashTests[Queue[Int]].hash) { - // default Arbitrary[BigDecimal] is a bit too intense :/ - implicit val arbBigDecimal: Arbitrary[BigDecimal] = - Arbitrary(arbitrary[Double].map(n => BigDecimal(n.toString))) checkAll("Order[BigDecimal]", OrderTests[BigDecimal].order) checkAll("CommutativeGroup[BigDecimal]", CommutativeGroupTests[BigDecimal].commutativeGroup) checkAll("CommutativeGroup[BigDecimal]", SerializableTests.serializable(CommutativeGroup[BigDecimal])) diff --git a/kernel/src/main/scala/cats/kernel/instances/BigDecimalInstances.scala b/kernel/src/main/scala/cats/kernel/instances/BigDecimalInstances.scala index 8406336d2b..439c59ad92 100644 --- a/kernel/src/main/scala/cats/kernel/instances/BigDecimalInstances.scala +++ b/kernel/src/main/scala/cats/kernel/instances/BigDecimalInstances.scala @@ -8,11 +8,20 @@ trait BigDecimalInstances { new BigDecimalGroup } +/** + * Note that combining, removing, and inverting `BigDecimal` values will use unlimited precision + * operations. + * + * This matches the behavior of Scala 2.12 and earlier versions, but not Scala 2.13, which means + * that `+` and `|+|` (or `sum` and `combineAll`) may not agree if you are working with values with + * limited-precision `MathContext`s. + */ class BigDecimalGroup extends CommutativeGroup[BigDecimal] { val empty: BigDecimal = BigDecimal(0) - def combine(x: BigDecimal, y: BigDecimal): BigDecimal = x + y - def inverse(x: BigDecimal): BigDecimal = -x - override def remove(x: BigDecimal, y: BigDecimal): BigDecimal = x - y + def combine(x: BigDecimal, y: BigDecimal): BigDecimal = new BigDecimal(x.bigDecimal.add(y.bigDecimal), x.mc) + def inverse(x: BigDecimal): BigDecimal = new BigDecimal(x.bigDecimal.negate(), x.mc) + override def remove(x: BigDecimal, y: BigDecimal): BigDecimal = + new BigDecimal(x.bigDecimal.subtract(y.bigDecimal), x.mc) } class BigDecimalOrder extends Order[BigDecimal] with Hash[BigDecimal] {