• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Casting int to double is failing

 
Ranch Hand
Posts: 50
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Error:

AverageInts.java:40: error: incompatible types: possible lossy conversion from double to float



What do I need to do to get that working?

 
Rancher
Posts: 167
7
Mac OS X IntelliJ IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
comment out line 40 and print out just the (sum / count), this should give you some idea what you are doing wrong

however this error message comes from the fact that you are trying to fit a double in a float
 
Master Rancher
Posts: 3539
39
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ken Austin
Ranch Hand
Posts: 50
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,

That did it. I changed the average type to a double and that solved the problem. I can't believe I missed that.

Thank you so much.

Ken
 
Marshal
Posts: 69494
277
  • Likes 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why anybody bothers with floats at all. Unless some other code requires a float.
 
Ranch Hand
Posts: 127
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Simmons
Master Rancher
Posts: 3539
39
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
https://coderanch.com/t/731949/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic