permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Floats Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Floats" Watch "Floats" New topic
Author

Floats

Tim Holmes
Greenhorn

Joined: Aug 26, 2005
Posts: 25
Why does f equal 2.0 here instead of 2.6?

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41634
    
  55
Both a and b are integers, so the division is an integer division (which involves throwing away the decimal places). Something like "a / (float) b" will force a floating-point division.


Ping & DNS - my free Android networking tools app
Hiral Amodia
Greenhorn

Joined: May 14, 2008
Posts: 2
Yeah... Ulf is perfectly right....
what is happening is :-


int a=8;
int b=3;
float f =(float) (a++/b--);

Thus (a++/b--)first gives an int result as 2 which is then casted to float makint it 2.0
Tim Holmes
Greenhorn

Joined: Aug 26, 2005
Posts: 25
That makes sense (casting int to a float). Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Floats