summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorclhodapp <clhodapp1@gmail.com>2014-01-05 21:38:26 -0600
committerclhodapp <clhodapp1@gmail.com>2014-01-06 13:07:30 -0600
commitb46d7aefd6eda36454cfd4cf339642e3c13c2022 (patch)
tree9359cafc6697ee0d7343b8ca3c6d19c1714dba21
parent527fd9aea58cf5c1b8f638d0321a8d0947d2916a (diff)
downloadscala-b46d7aefd6eda36454cfd4cf339642e3c13c2022.tar.gz
scala-b46d7aefd6eda36454cfd4cf339642e3c13c2022.tar.bz2
scala-b46d7aefd6eda36454cfd4cf339642e3c13c2022.zip
SI-8102 -0.0.abs must equal 0.0
SI-8102 points out that -0.0.abs returns -0.0 (in error). The same issue exists for -0.0f. This commit fixes the issue for both by delegating to math.abs.
-rw-r--r--src/library/scala/math/Numeric.scala4
-rw-r--r--test/junit/scala/math/NumericTest.scala18
2 files changed, 22 insertions, 0 deletions
diff --git a/src/library/scala/math/Numeric.scala b/src/library/scala/math/Numeric.scala
index e6644c0dfc..eafbf96993 100644
--- a/src/library/scala/math/Numeric.scala
+++ b/src/library/scala/math/Numeric.scala
@@ -127,6 +127,8 @@ object Numeric {
def toLong(x: Float): Long = x.toLong
def toFloat(x: Float): Float = x
def toDouble(x: Float): Double = x.toDouble
+ // logic in Numeric base trait mishandles abs(-0.0f)
+ override def abs(x: Float): Float = math.abs(x)
}
trait FloatIsFractional extends FloatIsConflicted with Fractional[Float] {
def div(x: Float, y: Float): Float = x / y
@@ -149,6 +151,8 @@ object Numeric {
def toLong(x: Double): Long = x.toLong
def toFloat(x: Double): Float = x.toFloat
def toDouble(x: Double): Double = x
+ // logic in Numeric base trait mishandles abs(-0.0)
+ override def abs(x: Double): Double = math.abs(x)
}
trait DoubleIsFractional extends DoubleIsConflicted with Fractional[Double] {
def div(x: Double, y: Double): Double = x / y
diff --git a/test/junit/scala/math/NumericTest.scala b/test/junit/scala/math/NumericTest.scala
new file mode 100644
index 0000000000..4f0657f471
--- /dev/null
+++ b/test/junit/scala/math/NumericTest.scala
@@ -0,0 +1,18 @@
+
+
+import org.junit.Assert._
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+
+@RunWith(classOf[JUnit4])
+class NumericTest {
+
+ /* Test for SI-8102 */
+ @Test
+ def testAbs {
+ assertTrue(-0.0.abs equals 0.0)
+ assertTrue(-0.0f.abs equals 0.0f)
+ }
+}
+