This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about a float value

 
Justen Hill
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int a = 8;
int b = 3;
float c = a++/b--;
System.out.println(c);

Why does this produce 2.0? Shouldn't the result be able to handle a more accurate answer?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about at what point in the calculatiuon of your expression the implicit conversion from int to float occurs.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say that that is pretty darn accurate. In fact, I would say it is precise.

what would you get if you did this?

int a = 8;
int b = 3;
int c = a++/b--;
float d = c;
System.out.println(d);
 
Justen Hill
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see it now. The division of the ints occurs first, then the result is stuffed in a float.

Is it actually a float since it doesn't have an 'f' assignment or is it a double?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic