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 remainder operation fails on math.Pi for large numbers #4981

Closed
scabug opened this issue Sep 9, 2011 · 3 comments
Closed

BigDecimal remainder operation fails on math.Pi for large numbers #4981

scabug opened this issue Sep 9, 2011 · 3 comments
Assignees

Comments

@scabug
Copy link

@scabug scabug commented Sep 9, 2011

Runing the following in Scala 2.9.1 or more recent:

BigDecimal("23953295732957329573295379352353252353253") % BigDecimal(math.Pi)

results in:

java.lang.ArithmeticException: Division impossible
at java.math.BigDecimal.divideToIntegralValue(BigDecimal.java:1783)
at java.math.BigDecimal.divideAndRemainder(BigDecimal.java:1916)
at java.math.BigDecimal.remainder(BigDecimal.java:1850)
at scala.math.BigDecimal.remainder(BigDecimal.scala:256)
at scala.math.BigDecimal.$percent(BigDecimal.scala:260)
at .(:8)
at .()
at .(:11)
at .()
at $print()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920)
at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
at scala.tools.nsc.io.package$$anon$2.run(package.scala:25)
at java.lang.Thread.run(Thread.java:662)

Whereas in 2.8 you get:

res0: scala.math.BigDecimal = 0.513676093799207

This has to do with the addition of the MathContext object. That object was added to limit decimal expansion of, e.g. BigDecimal(1) / BigDecimal(3), but introduces problems for the remainder operation.

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Sep 9, 2011

Imported From: https://issues.scala-lang.org/browse/SI-4981?orig=1
Reporter: @non
Affected Versions: 2.9.1

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Sep 14, 2011

@non said:
By the way, I have submitted a pull request to fix this issue on Github:

scala/scala#86

The pull also includes BigDecimal tests to catch this issue and older BigDecimal bugs.

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Sep 28, 2011

Commit Message Bot (anonymous) said:
(extempore in r25756) BigDecimal adjustments.

More sensible use of MathContext, plus some BigDecimal tests.
Contributed by Erik Osheim. Closes #4981, no review.

@scabug scabug closed this Sep 28, 2011
szeiger added a commit to szeiger/scala that referenced this issue Dec 11, 2018
This reverts commit ace11c0.

We did the same thing before in 3a1332c
to avoid problems such as scala/bug#4981
(back then) and scala/bug#11152 (now).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.