File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Floats

 
Tim Holmes
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does f equal 2.0 here instead of 2.6?

 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hiral Amodia
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes sense (casting int to a float). Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic