Skip to content

Commit

Permalink
Merge pull request #3581 from Ichoran/issue/3235-minimal
Browse files Browse the repository at this point in the history
SI-3235 math.round() returns wrong results for Int and Long
  • Loading branch information
adriaanm committed Feb 25, 2014
2 parents 5ae2fe0 + 1994a2d commit ec4479a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/library/scala/math/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,22 @@ package object math {
*/
def pow(x: Double, y: Double): Double = java.lang.Math.pow(x, y)

/** Returns the closest `long` to the argument.
/** There is no reason to round a `Long`, but this method prevents unintended conversion to `Float` followed by rounding to `Int`. */
@deprecated("This is an integer type; there is no reason to round it. Perhaps you meant to call this with a floating-point value?", "2.11.0")
def round(x: Long): Long = x

/** Returns the closest `Int` to the argument.
*
* @param x a floating-point value to be rounded to a `long`.
* @return the value of the argument rounded to the nearest`long` value.
* @param x a floating-point value to be rounded to a `Int`.
* @return the value of the argument rounded to the nearest `Int` value.
*/
def round(x: Float): Int = java.lang.Math.round(x)

/** Returns the closest `Long` to the argument.
*
* @param x a floating-point value to be rounded to a `Long`.
* @return the value of the argument rounded to the nearest`long` value.
*/
def round(x: Double): Long = java.lang.Math.round(x)

def abs(x: Int): Int = java.lang.Math.abs(x)
Expand Down
4 changes: 4 additions & 0 deletions src/library/scala/runtime/RichInt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ final class RichInt(val self: Int) extends AnyVal with ScalaNumberProxy[Int] wit
override def max(that: Int): Int = math.max(self, that)
override def min(that: Int): Int = math.min(self, that)
override def signum: Int = math.signum(self)

/** There is no reason to round an `Int`, but this method is provided to avoid accidental loss of precision from a detour through `Float`. */
@deprecated("This is an integer type; there is no reason to round it. Perhaps you meant to call this on a floating-point value?", "2.11.0")
def round: Int = self

def toBinaryString: String = java.lang.Integer.toBinaryString(self)
def toHexString: String = java.lang.Integer.toHexString(self)
Expand Down
4 changes: 4 additions & 0 deletions src/library/scala/runtime/RichLong.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ final class RichLong(val self: Long) extends AnyVal with IntegralProxy[Long] {
override def max(that: Long): Long = math.max(self, that)
override def min(that: Long): Long = math.min(self, that)
override def signum: Int = math.signum(self).toInt

/** There is no reason to round a `Long`, but this method is provided to avoid accidental conversion to `Int` through `Float`. */
@deprecated("This is an integer type; there is no reason to round it. Perhaps you meant to call this on a floating-point value?", "2.11.0")
def round: Long = self

def toBinaryString: String = java.lang.Long.toBinaryString(self)
def toHexString: String = java.lang.Long.toHexString(self)
Expand Down
12 changes: 12 additions & 0 deletions test/files/run/t3235-minimal.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
t3235-minimal.scala:3: warning: method round in class RichInt is deprecated: This is an integer type; there is no reason to round it. Perhaps you meant to call this on a floating-point value?
assert(123456789.round == 123456789)
^
t3235-minimal.scala:4: warning: method round in package math is deprecated: This is an integer type; there is no reason to round it. Perhaps you meant to call this with a floating-point value?
assert(math.round(123456789) == 123456789)
^
t3235-minimal.scala:5: warning: method round in class RichLong is deprecated: This is an integer type; there is no reason to round it. Perhaps you meant to call this on a floating-point value?
assert(1234567890123456789L.round == 1234567890123456789L)
^
t3235-minimal.scala:6: warning: method round in package math is deprecated: This is an integer type; there is no reason to round it. Perhaps you meant to call this with a floating-point value?
assert(math.round(1234567890123456789L) == 1234567890123456789L)
^
1 change: 1 addition & 0 deletions test/files/run/t3235-minimal.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-deprecation
8 changes: 8 additions & 0 deletions test/files/run/t3235-minimal.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
object Test {
def main(args: Array[String]) {
assert(123456789.round == 123456789)
assert(math.round(123456789) == 123456789)
assert(1234567890123456789L.round == 1234567890123456789L)
assert(math.round(1234567890123456789L) == 1234567890123456789L)
}
}

0 comments on commit ec4479a

Please sign in to comment.