• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

switch,if conditions

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai friends,
I have a seires of problems.
1)
int i1=5;
float f1=5.0f ,f2 =5.1f;
double d1=5.0 ,d2=5.1;
i used if condition to check like this..
if(i1==f1) gave me true
if(f1==d1) gave me true
if(f2==d2) amazingly false . why??
plz explain me whatz the prob actually.
-----------------------------
2) inside the switch if i want to use float values how can we do that.
------------------------------
3) just look at the following.
float f= 0.20f; double d=0.8; System.out.println(f+d);
can u tell me whats the o/p.
Do u think it is 1.000 (or like that)
Exactly No.
if u don't believe me write a prgm and check it out.
it will display : 1.0000000029802323
whatz actually this. it is suppose to be 1.000(say).
Any tech-savy there to solve my doubt.
------------------
4)
Guyz, will u plz explain me how to compute shift operations on

hexadecimal ints like
int i1 = 0xffffffff, i2 = i1 << 1;i3=i1>>1; etc

thanks
regards,
Mahesh
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Certain floating point numbers, e.g. 0.1 decimal, cannot be represented exactly in binary. Think about it. 0.1 binary is 0.5 decimal; 0.01 binary is 0.25 decimal; 0.001 binary is 0.125 decimal. They all end in 5 and always will.
2) You can't, unless you convert to int in some satisfactory way.
3) See 1)
4) Try searching for a tutorial. Sun's website is a good start.

Jules
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a rule, always distrust any attempt to compare floating point values for equality. The only instance where this is safe is when you want to see if an existing float or double has changed. In all other cases, you must be prepared to ignore a certain delta due to precision limitations, e.g. - Peter
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For 2) not possible
For 3) try using BigDecimal class
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic