diff options
author | Adriaan Moors <adriaan.moors@typesafe.com> | 2014-02-25 11:35:44 -0800 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2014-02-25 11:35:44 -0800 |
commit | ec4479aa7d0279daa701481fb7d77c1f43617190 (patch) | |
tree | d2503ba92f1f6e22148e25b0775a6f13e2ad6a0c /src | |
parent | 5ae2fe02722d637c1d2683d18918c8a2e1ab3425 (diff) | |
parent | 1994a2d901e1ca948a53b3d00df9017782c47b5c (diff) | |
download | scala-ec4479aa7d0279daa701481fb7d77c1f43617190.tar.gz scala-ec4479aa7d0279daa701481fb7d77c1f43617190.tar.bz2 scala-ec4479aa7d0279daa701481fb7d77c1f43617190.zip |
Merge pull request #3581 from Ichoran/issue/3235-minimal
SI-3235 math.round() returns wrong results for Int and Long
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/math/package.scala | 16 | ||||
-rw-r--r-- | src/library/scala/runtime/RichInt.scala | 4 | ||||
-rw-r--r-- | src/library/scala/runtime/RichLong.scala | 4 |
3 files changed, 21 insertions, 3 deletions
diff --git a/src/library/scala/math/package.scala b/src/library/scala/math/package.scala index af5dad5c38..58ece8a05b 100644 --- a/src/library/scala/math/package.scala +++ b/src/library/scala/math/package.scala @@ -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) diff --git a/src/library/scala/runtime/RichInt.scala b/src/library/scala/runtime/RichInt.scala index cc4e92dfa3..cda9d2907a 100644 --- a/src/library/scala/runtime/RichInt.scala +++ b/src/library/scala/runtime/RichInt.scala @@ -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) diff --git a/src/library/scala/runtime/RichLong.scala b/src/library/scala/runtime/RichLong.scala index df0bbec047..b405fcda3d 100644 --- a/src/library/scala/runtime/RichLong.scala +++ b/src/library/scala/runtime/RichLong.scala @@ -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) |