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 and IterableOnce.sum #11592

Closed
dwijnand opened this issue Jun 26, 2019 · 2 comments

Comments

Projects
None yet
4 participants
@dwijnand
Copy link
Member

commented Jun 26, 2019

There is second problem with sum:

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> :paste
// Entering paste mode (ctrl-D to finish)

import java.math.MathContext

object BigDecimalTest {
  def main(args: Array[String]): Unit = {
    test()
  }

  def test(): Unit = {

     val bds = List(
     scala.math.BigDecimal("1000000000000000000000000.1", MathContext.UNLIMITED),
     scala.math.BigDecimal("9.0000000000000000000000009", MathContext.UNLIMITED))

     assert(bds.sum == scala.math.BigDecimal("1000000000000000000000009.1000000000000000000000009", MathContext.UNLIMITED))

     // Below line works with scala 2.13.0
     //assert(bds.foldLeft(BigDecimal(0, MathContext.UNLIMITED))(_ + _) == scala.math.BigDecimal("1000000000000000000000009.1000000000000000000000009", MathContext.UNLIMITED))
     println("BigDecimal works")
  }
}

// Exiting paste mode, now interpreting.

import java.math.MathContext
defined object BigDecimalTest

scala> BigDecimalTest.test
java.lang.AssertionError: assertion failed
  at scala.Predef$.assert(Predef.scala:267)
  at BigDecimalTest$.test(<console>:14)
  ... 28 elided

Which is probably caused by wrong math context with num.zero in here: https://github.com/scala/scala/blob/6b4d32c3f518d21a798e8d3cf4a8c35866afa8e2/src/library/scala/collection/IterableOnce.scala#L915

I can't stress enough that this change with 2.13 is highly surprising, even though orinal implementation with 2.11 and 2.12 has been broken.

P.S. Above code works with 2.12.8

Originally posted by @35VLG84 in #11590 (comment)

@35VLG84

This comment has been minimized.

Copy link

commented Jun 26, 2019

As @Ichoran pointed out in #11590 (comment) this applies also for product, which is broken on 2.11.12, 2.12.8 and 2.13.0:

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> :paste
// Entering paste mode (ctrl-D to finish)

import java.math.MathContext

val bds = List(
   scala.math.BigDecimal("1000000000000000000000000.1", MathContext.UNLIMITED),
   scala.math.BigDecimal("9.00000000000000000000000091", MathContext.UNLIMITED))

val prod = bds.foldLeft(BigDecimal(1, MathContext.UNLIMITED))(_ * _)
assert(prod.toString == "9000000000000000000000001.810000000000000000000000091")

// Exiting paste mode, now interpreting.

import java.math.MathContext
bds: List[scala.math.BigDecimal] = List(1000000000000000000000000.1, 9.00000000000000000000000091)
prod: scala.math.BigDecimal = 9000000000000000000000001.810000000000000000000000091

scala> bds.product
res1: scala.math.BigDecimal = 9000000000000000000000001.810000000

scala> assert(prod == bds.product)
java.lang.AssertionError: assertion failed
  at scala.Predef$.assert(Predef.scala:267)
  ... 28 elided
@Ichoran

This comment has been minimized.

Copy link

commented Jun 26, 2019

For reference, my suggested fix is to use reduceOption rather than foldLeft to implement these. (If someone doesn't like the overhead, then something like

val i = xs.iterator
if (!i.hasNext) num.one
else {
  var x = i.next
  while (i.hasNext) x = num.times(x, i.next)
  x
}

for IterableOnce, with maybe an override for efficiency in List and IndexedSeq.)

pull bot pushed a commit to Pandinosaurus/scala that referenced this issue Jul 15, 2019

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.