Skip to content

Precision loss due to numeric conversions #2289

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

Closed
dwijnand opened this issue Apr 21, 2017 · 4 comments
Closed

Precision loss due to numeric conversions #2289

dwijnand opened this issue Apr 21, 2017 · 4 comments

Comments

@dwijnand
Copy link
Member

Welcome to Scala.next (pre-alpha, git-hash: unknown)  (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_121).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val x = 123456789L
val x: Long = 123456789
scala> val y = 0F
val y: Float = 0.0f
scala> x - (if (true) x else y)
val res0: Float = -3.0f
@odersky
Copy link
Contributor

odersky commented Apr 21, 2017

Isn't that as spec'ed? Current Scala gives the same result, one thing we specifically do not want to do is change the runtime behavior of dotty as compared to Scala.

@odersky odersky closed this as completed Apr 21, 2017
@dwijnand
Copy link
Member Author

I agree we can't change runtime behaviour without a suitable migration path.

Talking with @aphyr we were thinking that perhaps one solution could be to:

  1. by default insert runtime checks to fail early rather than return the wrong result
  2. unless you can determine at compile time that the conversion is safe, so for example a 0 literal would convert without any cost, and
  3. explicit conversions (e.g .toFloat) could be used in performance-critical code, avoiding the runtime check

WDYT?

@smarter
Copy link
Member

smarter commented Apr 21, 2017

Another possibility would be to never convert Long to Double and Int to Float, and instead use union types. I suspect this won't affect too much code since Long and Float are rarely used compared to Int and Double (where you can convert without losing precision).

@dwijnand
Copy link
Member Author

Happy to defer to #2451.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants