Javatpoint Logo

what is difference between float and double

By: raghav*** On: Fri Jul 04 15:37:13 IST 2014     Question Reputation1 Answer Reputation0 Quiz Belt Series Points0  1Blank User
what is difference between float and double and which one is flexible and why float have more roundtrips and Up0Down
core java  x  328Tags

 

In both cases your code is comparing a float value to a double, as the compiler sees 3.2f as a float
and 3.2 (without the f) as a double.

The float data type is a single-precision 32-bit
The double data type is a double-precision 64-bit

You shouldn't ever compare floats or doubles for equality, you can't really guarantee that the number you assign to the float or double is exact.

It's the same for float and double - both are binary floating point types, but double has more precision than float.

By default java assumes a manually specified number to be a double, as a double would more precisely cover more numbers that you could type in. You have to cast between doubles and float explicitly.
Image Created0Down

By: [email protected] On: Fri Jul 04 17:40:48 IST 2014 Question Reputation0 Answer Reputation392 Belt Series Points0 392User Image
Are You Satisfied :2Yes1No
 
A float is a 32 bit IEEE 754 floating point.

A double is a 64 bit IEEE 754 floating point.

so it is just a matter of precision because neither of the fraction portions .8 and .65 have a terminating binary representation, so there is some rounding error. the double has more precision so it has slightly less rounding error.
Image Created0Down

By: [email protected] On: Sat Jul 05 13:18:28 IST 2014 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :1Yes2No
 
We can represent float variable value up to 7 digits after decimal point, whereas double value can be represent up to 15 digits after decimal point.Image Created0Down

By: [email protected] On: Tue Nov 07 12:44:28 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No