Skip to content

Commit b46d7ae

Browse files
committed
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.
1 parent 527fd9a commit b46d7ae

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/library/scala/math/Numeric.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ object Numeric {
127127
def toLong(x: Float): Long = x.toLong
128128
def toFloat(x: Float): Float = x
129129
def toDouble(x: Float): Double = x.toDouble
130+
// logic in Numeric base trait mishandles abs(-0.0f)
131+
override def abs(x: Float): Float = math.abs(x)
130132
}
131133
trait FloatIsFractional extends FloatIsConflicted with Fractional[Float] {
132134
def div(x: Float, y: Float): Float = x / y
@@ -149,6 +151,8 @@ object Numeric {
149151
def toLong(x: Double): Long = x.toLong
150152
def toFloat(x: Double): Float = x.toFloat
151153
def toDouble(x: Double): Double = x
154+
// logic in Numeric base trait mishandles abs(-0.0)
155+
override def abs(x: Double): Double = math.abs(x)
152156
}
153157
trait DoubleIsFractional extends DoubleIsConflicted with Fractional[Double] {
154158
def div(x: Double, y: Double): Double = x / y
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
import org.junit.Assert._
4+
import org.junit.Test
5+
import org.junit.runner.RunWith
6+
import org.junit.runners.JUnit4
7+
8+
@RunWith(classOf[JUnit4])
9+
class NumericTest {
10+
11+
/* Test for SI-8102 */
12+
@Test
13+
def testAbs {
14+
assertTrue(-0.0.abs equals 0.0)
15+
assertTrue(-0.0f.abs equals 0.0f)
16+
}
17+
}
18+

0 commit comments

Comments
 (0)