Permalink
Browse files

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.
  • Loading branch information...
1 parent 527fd9a commit b46d7aefd6eda36454cfd4cf339642e3c13c2022 @clhodapp clhodapp committed Jan 6, 2014
Showing with 22 additions and 0 deletions.
  1. +4 −0 src/library/scala/math/Numeric.scala
  2. +18 −0 test/junit/scala/math/NumericTest.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
@@ -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)
+ }
+}
+

0 comments on commit b46d7ae

Please sign in to comment.