• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

float & JQ+ -- VELU

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Velu's notes & JQ+ says that line 3 will return false but it returns true when compiling with jdk1.3 . why is it so?
public class vo
{
public static void main(String[] a)
{
float f=1.0f/3.0f;
System.out.println(f);
System.out.println(f*3.0f);
System.out.println((f*3.0f)==1.0f);//3
}
}
i guess this is because jdk1.2 gives that output. But in the actual exam what should be the correct answer to the question ?
Thx in advance
 
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
The point is that you can't rely on exact representation of repeating fractions and decimal representations in floating point primitives. Just watch out for assuming that == will always work as you expect after a series of floating point operations.
Bill
 
reply
    Bookmark Topic Watch Topic
  • New Topic