jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt about a float value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt about a float value" Watch "Doubt about a float value" New topic

Doubt about a float value

Justen Hill

Joined: Nov 07, 2007
Posts: 5
int a = 8;
int b = 3;
float c = a++/b--;

Why does this produce 2.0? Shouldn't the result be able to handle a more accurate answer?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63868

Think about at what point in the calculatiuon of your expression the implicit conversion from int to float occurs.

[Asking smart questions] [About Bear] [Books by Bear]
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

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;

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Justen Hill

Joined: Nov 07, 2007
Posts: 5
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?
I agree. Here's the link: http://aspose.com/file-tools
subject: Doubt about a float value
It's not a secret anymore!