*
The moose likes Java in General and the fly likes Difference between float f & double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference between float f & double" Watch "Difference between float f & double" New topic
Author

Difference between float f & double

sahan thinusha
Greenhorn

Joined: Nov 10, 2012
Posts: 10


Please explain about these commented lines..
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

What do you want to know about them? float and double are both floating point primitive types, but double uses twice as many bits to store the value and so can store numbers more precisely.
sahan thinusha
Greenhorn

Joined: Nov 10, 2012
Posts: 10

f=0.123456789123456789F;//compile
but, f=0.123456789123456789;//compile error
why?
and (f=0.1234567......F;)=(d=0.1234567.........;) or (f=0.1234567......F;)<(d=0.1234567.........;) please explain this.






Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sahan thinusha wrote:f=0.123456789123456789F;//compile
but, f=0.123456789123456789;//compile error
why?


A floating point literal in Java is a double unless you tell the compiler otherwise with the F. So it's like you're doing and when you assign a double value to a float variable, you can lose precision, so the compiler requires you to cast.

and (f=0.1234567......F;)=(d=0.1234567.........;) or (f=0.1234567......F;)<(d=0.1234567.........;) please explain this.


It's not really clear what you're asking, but if you're asking whether a float will be equal to a less than a double intiialized to the same literal, the answer is, it depends. If the number can be represented exactly in a float, they'll be equal. If it cannot be, they might be different, because the double has more precision.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between float f & double
 
Similar Threads
converting float to double
data types
float to double conversion
What is the difference between a double and a float?