• 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

Conversions

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why on comparing, I get the output Unequal when f is supposed to get promoted to double

Code:
----------------------------------------------------------------------
float f=12.3f;
if(f==12.3){
out.println("Equal");
}
else{
out.println("Unequal");
}
----------------------------------------------------------------------
Output: Unequal.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do not use == with float(double) variable

public class class22 {

public static void main(String[] args) {

double f = 12.3f;
System.out.println(f);
if (f == 12.3) {
System.out.println("Equal");
} else {
System.out.println("Unequal");
}

}
}

12.300000190734863
Unequal
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ritu,
Well, in Java, a number declared as a float is not the same as a double. 12.3f is a float while 12.3 is a double value.

Hence the unequality !!
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ritu Kapoor:
Why on comparing, I get the output Unequal when f is supposed to get promoted to double



Ok promotion of float to double is happening. But it is not that which is the cause of the Unequal .This is a case of - how float and double are represented . See the site

Floating Representation for detailed information.


The following example clears this:




Output is :
6.289999952316284 // float added to double - approximate representation.
6.29 // double added to double is more precise


--
Shivani
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ritu,
in java float are 32 bits and doubles are 64 bits.
neither of them store accutrate value, but approxmate values.
Here double stores values of more precision(more closer to actual values) than floats so there is inequality while comparing these values.

want to know in more detail?
IEEE 754

hope this helps you.
 
reply
    Bookmark Topic Watch Topic
  • New Topic