Look at the three lines of code below.
float f = 1;
float g = 1.1;
float h = 1.1f;
Second line has compilation errors, while the other lines do not have compilation errors. First line is working fine without suffix f and third line is working with suffix f. Why is this?
First line automatically casts int
to float
(ok).
Second line could not cast double
to float
because of loss of precision. You can make an explicit cast:
float g = (float) 1.1;
Third line does not need modification.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With