Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import org.apache.spark.util.StatCounter
class DoubleRDDFunctions(self: RDD[Double]) extends Logging with Serializable {
/** Add up the elements in this RDD. */
def sum(): Double = {
self.reduce(_ + _)
self.fold(0.0)(_ + _)
}

/**
Expand Down
7 changes: 7 additions & 0 deletions core/src/test/scala/org/apache/spark/rdd/DoubleRDDSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
package org.apache.spark.rdd

import org.scalatest.FunSuite
import org.scalatest.Matchers._

import org.apache.spark._

class DoubleRDDSuite extends FunSuite with SharedSparkContext {
test("sum") {
sc.parallelize(Seq.empty[Double]).sum() should be(0.0 +- 0.0001)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just use assert here as int he rest of the file. Also I think you can assert exact equality in these cases. It really should be 3.0 in the last instance, precisely.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Experience thought me that exact equality with doubles is unreliable. Now I err on the safe side and always use inexact matchers in unit tests.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doubles aren't unstable; 0.0 == 0.0 always. Yes i know what you mean but in these cases we can expect the result to exact to machine precision, always.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't say unstable :)
Anyway, changed as requested.

sc.parallelize(Seq(1.0)).sum() should be(1.0 +- 0.0001)
sc.parallelize(Seq(1.0, 2.0)).sum() should be(3.0 +- 0.0001)
}

// Verify tests on the histogram functionality. We test with both evenly
// and non-evenly spaced buckets as the bucket lookup function changes.
test("WorksOnEmpty") {
Expand Down