Details

Type: Bug

Status: CLOSED

Priority: Major

Resolution: Won't Fix

Affects Version/s: None

Fix Version/s: Scala 2.11.0RC1

Component/s: Library (Misc)

Labels:

Environment:
math round rounding int long double float conversion loss precision
Description
The problem is that math.round (== java.lang.Math.round) is only defined for Float and Double. If called with Int or Long arguments, they are converted to Float/Double, resulting in an loss of precision.
Example:
math.round(123456789)

res17: Int = 123456792

A way to fix it is adding two additional overloaded methods round() to the math object:
/** This method prevents the implicit type promotion of Long to Double, which would lead to a loss of precision. */
@deprecated("Rounding Long won't do anything. There is a high chance that calling this method is a bug in your code.")
def round(x: Long): Long = x
/** This method prevents the implicit type promotion of Int to Float, which would lead to a loss of precision. */
@deprecated("Rounding Int won't do anything. There is a high chance that calling this method is a bug in your code.")
def round(x: Int): Int = x
Another possibility, which might be more correct (but I haven't thought deeply about this):
Move math.round(x: Double) to the Double class as an instance method and math.round(x: Float) to the Float class as an instance method.