New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
deprecate round on Int and Long #3235
Comments
Imported From: https://issues.scala-lang.org/browse/SI-3235?orig=1 |
@soc said: /** 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. |
@paulp said: Personally I wish we could at least turn off primitives drifting from one type to another, but I already tried for that and martin did not see the value. |
@soc said:
But imho round() should either
|
@soc said: It seems that the only way to prevent the promotion of whole numbers to Doubles/Floats when using these methods is to overwrite them in Int and Long (option a) does not work). I can't think of a more appropriate solution at the moment, but maybe you have a better idea? (PPS: I wonder if there is any performance difference between math.round and RichDouble's round because of the implicit conversion from Double to RichDouble...) |
@dubochet said: |
@soc said: math.round(_x_L) // x being a sufficiently small/large number But that's a Java feature, too. So I'll just leave it closed. |
@soc said:
Locke would call that "Argument from authority" :-) |
@adriaanm said: |
The problem is that math.round(n) (== java.lang.Math.round(n)) 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:
The text was updated successfully, but these errors were encountered: