Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigDecimal mod (`%`) behavior change in 2.13 vs 2.12 on certain multiples #11597

Open
changlinli opened this issue Jun 27, 2019 · 1 comment

Comments

Projects
None yet
2 participants
@changlinli
Copy link

commented Jun 27, 2019

I suspect the entire idea of mod for BigDecimals is fraught with danger (at least in the current implementation) and has been across multiple Scala versions, so I'm not sure if this is a bug per se.

Nonetheless, it's surprising and any discussion of this might help others. So here goes:

Scala 2.12

Welcome to Scala 2.12.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_212).
Type in expressions for evaluation. Or try :help.

scala> val x = BigDecimal(new java.math.BigDecimal("1E+136"))
x: scala.math.BigDecimal = 1E+136

scala> val upperLimit = BigDecimal(11)
upperLimit: scala.math.BigDecimal = 11

scala> x % upperLimit
res0: scala.math.BigDecimal = 1

scala> val upperLimit = BigDecimal(10)
upperLimit: scala.math.BigDecimal = 10

scala> x % upperLimit
res1: scala.math.BigDecimal = 0E+135

Scala 2.13

Welcome to Scala 2.13.0 (OpenJDK 64-Bit Server VM, Java 1.8.0_212).
Type in expressions for evaluation. Or try :help.

scala> val x = BigDecimal(new java.math.BigDecimal("1E+136"))
x: scala.math.BigDecimal = 1E+136

scala> val upperLimit = BigDecimal(11)
upperLimit: scala.math.BigDecimal = 11

scala> x % upperLimit
java.lang.ArithmeticException: Division impossible
  at java.math.BigDecimal.divideToIntegralValue(BigDecimal.java:1857)
  at java.math.BigDecimal.divideAndRemainder(BigDecimal.java:1989)
  at java.math.BigDecimal.remainder(BigDecimal.java:1923)
  at scala.math.BigDecimal.remainder(BigDecimal.scala:525)
  at scala.math.BigDecimal.$percent(BigDecimal.scala:529)
  ... 36 elided

scala> val upperLimit = BigDecimal(10)
upperLimit: scala.math.BigDecimal = 10

scala> x % upperLimit
res1: scala.math.BigDecimal = 0E+135
@dwijnand

This comment has been minimized.

Copy link
Member

commented Jun 28, 2019

See #11590, IIUC this is because of the MathContext, indeed this works like 2.12.8, on 2.13.0:

Welcome to Scala 2.13.0 (OpenJDK GraalVM CE 19.0.0, Java 1.8.0_212).
Type in expressions for evaluation. Or try :help.

scala> val x = BigDecimal("1E+136", java.math.MathContext.UNLIMITED)
x: scala.math.BigDecimal = 1E+136

scala> val upperLimit = BigDecimal(11, java.math.MathContext.UNLIMITED)
upperLimit: scala.math.BigDecimal = 11

scala> x % upperLimit
res0: scala.math.BigDecimal = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.