• 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

why it is false

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test
{
public static void main(String[] args)
{
double d = Integer.MAX_VALUE;
float f= Integer.MAX_VALUE;
if (f == d)
System.out.println("equal");
else
System.out.println(" not equal ");
}
}
it prints not equal however if i take float f = 10; and double d =10 its shows equal
please explain
thanks
NAveen
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Naveen Sharma:
public class Test
{
public static void main(String[] args)
{
double d = Integer.MAX_VALUE;
float f= Integer.MAX_VALUE;
if (f == d)
System.out.println("equal");
else
System.out.println(" not equal ");
}
}
it prints not equal however if i take float f = 10; and double d =10 its shows equal
please explain
thanks
NAveen


Add lines:

which should give you an idea why...
------------------
Antti Barck
It Solutions Consultant, NSD Oy
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although both float and int use 32 bits, float has to use some bits for the exponent, therefore it can't maintain the low significance bits in Integer.MAX_VALUE. However, double with 64 bits to play with can.
Bill
------------------
author of:
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In simple terms double becomes more precise with 64 bit info than that of float with 32 bit.
Hence whenever u do a calculation on RHS and assign the same to double and float they won't be equal.
Java Gurus pls correct me if the interpretation is wrong.
 
reply
    Bookmark Topic Watch Topic
  • New Topic