• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

why it is false

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
I will suppress my every urge. But not this shameless plug:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!