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
Weak conformance to Float and Double are incorrect #10773
Comments
Would it still be safe for |
I think so. That's why I wrote "Option 1". Option 2: Replace Long <:_w Float with Int <:_w DoublePro as noted above by @NthPortal:
|
Yes, widening |
Can we please widen literals that fit losslessly? It's a pain to have to write |
This didn't get entirely fixed by scala/scala#8679 / scala/scala#7405, there is no warning for literals yet.
cc @smarter, @som-snytt. |
Also this should warn:
|
@lrytz thanks, I noticed that from the t8450 test. I started looking at constants then got distracted by dotty harmonization. |
If you warn for that, then you should also warn for |
Right, float literals is a different issue, and not urgent. |
In Scala 3, you have to notice what was inferred. Hopefully, you'll have
The test is round-tripping. To warn on float, you can check the double.
I remember the parse issue scala/scala#5648 |
Follow-up will be at scala/scala#8730 My question is: How is It seems to me, it should warn on addition as though it were defined |
I'd say the main difference is that you don't even need an int to float conversion to be lossy for |
SLS 6.26.1 talks about Numeric Literal Narrowing
scala> Set[Byte](1, 2)
res0: scala.collection.immutable.Set[Byte] = Set(1, 2)
scala> Set[Byte](1, 2, 128)
^
error: type mismatch;
found : Int(128)
required: Byte Given that |
I stopped worrying about newbie traps like I did update the spec. I used the test for harmonization, but with more awkward words. A conceptual trap is that "precision" doesn't mean "significant digits". |
Related: https://contributors.scala-lang.org/t/can-we-get-rid-of-cooperative-equality/1131/85
Related: scala/scala#6398
steps
problem
expectation
It should not compile. Similar to using literal in
Set[Byte]
.note
Here's what SLS says: https://github.com/scala/scala/blob/2.13.x/spec/03-types.md
IEEE floats
https://en.wikipedia.org/wiki/IEEE_754 has nice writeup of IEEE floats. The useful part is the notion of significand. Float has 24 bits of it, and Double 53 bits.
Given that Int has 32 bits, Float is unable to represent all integers
Int
can represent.Same goes for
Long
vsDouble
.Option 1: Split the floats
My suggestion would be to remove "Long <:_w Float" from the spec.
The breaking change about this is that small integers like
0
and1
no longer will automatically "widen" toFloat
andDouble
, but I am personally ok with it.The text was updated successfully, but these errors were encountered: