• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

whats the difference between float & double

 
Ranch Hand
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all
I have got some question about float & double , my answer is due to precision because float is 32 bit integer and double is 64 bit integer, However I am not sure...please help me


Output :
different
same

In first case why it is not equal and in case the value is same,why is it so?
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are getting "different" for the 3.2 because a computer's floating point unit works with base 2 binary and 0.2 can't be represented precisely in binary, it is called a repeater fraction. In base-2 only numbers with denominators that are powers of 2 are terminating, which I think is only .25, .50, and .75, which is why 6.5 shows up as "same".

Hope this helps, but there may be better minded people out there that can give you a better explanation perhaps.
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Float and double values are not integers, but rather floating point numbers. The Java
integer types are: byte, short, int, long (and char - 16 bits unsigned). 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. I suspect the precision of the compare logic has
something to do with the different results. Someone else may comment further.

Jim ... ...
 
Vas Miriyala
Ranch Hand
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The float data type is a single-precision 32-bit
The double data type is a double-precision 64-bit
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to JLS - 3.10.2 Floating-Point Literals...

A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double...


So in line 6, f1 is assigned the value 3.2f, which is a float.

But in line 9, this value is compared to the value 3.2, which is a double.

These are not equal because the value 3.2 cannot be precisely stored (in binary) within the range of a float.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Ian pointed out, 6.5 can be represented exactly in binary, whereas 3.2 can't. That's why the difference in precision doesn't matter for 6.5, so 6.5 == 6.5f.

To quickly refresh how binary numbers work:

100 -> 4
10 -> 2
1 -> 1
0.1 -> 0.5 (or 1/2)
0.01 -> 0.25 (or 1/4)
etc.

6.5 in binary: 110.1 (exact result, the rest of the digits are just zeroes)
3.2 in binary: 11.001100110011001100110011001100110011001100110011001101... (here precision matters!)

A float only has 24 bits precision (the rest is used for sign and exponent), so:

3.2f in binary: 11.0011001100110011001100 (not equal to the double precision approximation)


There's a handy calculator for things like this at http://www.digitconvert.com/


Basically it's the same as when you're writing 1/5 and 1/7 in decimal numbers:

1/5 = 0,2
1,7 = 0,14285714285714285714285714285714...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A float is a 32-bit, single-precision IEEE 754 floating point number.

A double is a 64-bit, double-precision IEEE 754 floating point number.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Sebastian Hatt, and thank you for that useful first reply.
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic