• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt pertaining to float and double  RSS feed

 
RabiDas Sharma
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone
I'm going to ask a very silly doubt today.





please tell me why i'm not getting fractional part
as double is a fractional data holding variable its
supposed to give both integral and fractional part
just like float type variable.

what is the difference between float and double other than bit depth??

with regards.
thanks in advance.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not silly at all. It is a subtle difference, but it isn't about float vs double.



f1 and f3 print 1.25. f2 prints 1.0.

Here's what is going on in f2:
  • Java sees two integers and divides them. It says, integer division doesn't have decimals, so I'll just drop the .25
  • Java sees you want to put the result in a float. So it adds the 1.0
  • .

    These are separate events. By contrast, f1 and f3 both give Java a clue that you want a float during step one of this process. It's the same idea for doubles.

    Give it a shot for double so you can see that in practice. (leaving it as an exercise to you because I think you'll remember it better that way)
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!