float is a 32-bit IEEE 754 single precision Floating Point Number – 1 bit for the sign, 8 bits for the exponent, and 23* for the value. float has 7 decimal digits of precision. double is a 64-bit IEEE 754 double precision Floating Point Number – 1 bit for the sign, 11 bits for the exponent, and 52* bits for the value.
Though both Java float vs Double is approximate types, use double if you need more precise and accurate results. Use float if you have memory constraint because it takes almost half as much space as double. If your numbers cannot fit in the range offered by float, then use double.
Float has comparatively less precision and is used to store decimal numbers with fewer digits. Double has almost twice the precision as float and is used to store decimal numbers with more digits. It has a 32-bit floating-point precision according to IEEE.
Size: Float is of size 32 bits while double is of size 64 bits. Hence, double can handle much bigger fractional numbers than float. They differ in the allocation of bits for the representation of the number. Both float and double use 1 bit for representing the sign of the number.
When I run the following code ,
NSString* s= @"10000000.01";
float f = [s floatValue];
double d = [s doubleValue];
if(f > 10000000)
{
NSLog(@"Over Value");
}
else {
NSLog(@"OK Float");
}
if(d > 10000000)
{
NSLog(@"Over value");
}
else {
NSLog(@"OK Double");
}
The response is like following.
2013-04-19 17:07:29.284 float[2991:907] OK Float
2013-04-19 17:07:29.287 float[2991:907] Over value
Why float value changed to 10000000.00 instead of 10000000.01 ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With