I'm trying to do a cast from an to a double on line 40, and I can't figure out why it isn't working. The error messages seems to indicate that I am trying to convert a double to a float, but that's not the case.
I've Googled casting and my cast appears to match all of the examples that I've seen, including the example program from the text.
What am I missing that is causing the cast to fail in my program? Can someone give me an assist?
AverageInts.java:40: error: incompatible types: possible lossy conversion from double to float
Ken Austin wrote:The error messages seems to indicate that I am trying to convert a double to a float, but that's not the case.
And yet, I see:
Which is effectively trying to convert a double to a float. You probably need to either use doubles, or use floats. I would say there's almost never a need to use floats nowadays... maybe only if you are using large arrays and need to save some memory. But that' s pretty uncommon. Just use double, you'll be happier.
Campbell Ritchie wrote:I don't know why anybody bothers with floats at all. Unless some other code requires a float.
It always bothered me since I learned about floating point numbers in java: what is a legitimate use case for a float when javas base datatype is always a double in the first place?
From what I know when one declares a "float" like 0.0 it's a double right away - one doesn't have to the d suffix. Only when someone explicitly wants to assign a float the f suffix has to be appended: 0.0f
What would a reason for me to design some code require float at all?
Well, the designers of Java seemed to think it was important to use float rather than double in the java.graphics package, where you'll find classes like Font. And frankly they weren't wrong, it's an environment where the numbers used are not very big (maybe less than 1000) but they don't have to be integers. So six-ish decimal places is plenty.
Remember also that this package was in Java 1.0 already, so it was written about 1996. In those days memory was more important than it is now and Java was at risk of being called a "memory hog". So using float rather than double tries to avoid that.
posted 2 weeks ago
Paul Clapham wrote:Remember also that this package was in Java 1.0 already, so it was written about 1996. In those days memory was more important than it is now and Java was at risk of being called a "memory hog". So using float rather than double tries to avoid that.
Also, back then 32-bit architecture was the norm. So a 32-bit float was just one word, and a 64-bit double was two words, meaning that using floats could be significantly faster, both in terms of reading and writing, and in terms of performing operations such as multiplication. Nowadays, that's pretty much a non-issue - with 64-bit words, the speed for both float and double is likely the same.
posted 1 week ago
Since AWT and Swing don't seem to have been updated for some time, are Oracle going to bother about those floats? But why you would need a float for the load factor in hash maps or hash sets baffles me, when it would be easy to overload the constructor to take doubles.
I am going to test your electrical conductivity with this tiny ad:
ScroogeXHTML 9.0 - RTF to HTML5 and XHTML converter