By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,404 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,404 IT Pros & Developers. It's quick & easy.

Unpredictable Answer.

P: 34
Hello All,
I was just trying to understand concept of float and double, when I write this code I got strange output:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     float a;
  7.     double b;
  8.     a=0.7;
  9.     b=0.7;
  10.     if(a<0.7)
  11.         cout<<"A"<<endl;
  12.     else
  13.         cout<<"B"<<endl;
  14.     if(b<0.7)
  15.         cout<<"A"<<endl;
  16.     else
  17.         cout<<"B"<<endl;
  18.     return 0;
  19. }
  20.  
  21.  
OUTPUT:

Expand|Select|Wrap|Line Numbers
  1. A
  2. B
  3.  
But I think first 'A' should not get printed, because a=0.7, how can if statement be true.
Can anyone please help me to find out my error.

Suyash
Jun 16 '07 #1
Share this Question
Share on Google+
2 Replies


ilikepython
Expert 100+
P: 844
Hello All,
I was just trying to understand concept of float and double, when I write this code I got strange output:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     float a;
  7.     double b;
  8.     a=0.7;
  9.     b=0.7;
  10.     if(a<0.7)
  11.         cout<<"A"<<endl;
  12.     else
  13.         cout<<"B"<<endl;
  14.     if(b<0.7)
  15.         cout<<"A"<<endl;
  16.     else
  17.         cout<<"B"<<endl;
  18.     return 0;
  19. }
  20.  
  21.  
OUTPUT:

Expand|Select|Wrap|Line Numbers
  1. A
  2. B
  3.  
But I think first 'A' should not get printed, because a=0.7, how can if statement be true.
Can anyone please help me to find out my error.

Suyash
You may want to take a look at this thread .
Jun 16 '07 #2

P: 14
Hello All,
I was just trying to understand concept of float and double, when I write this code I got strange output:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     float a;
  7.     double b;
  8.     a=0.7;
  9.     b=0.7;
  10.     if(a<0.7)
  11.         cout<<"A"<<endl;
  12.     else
  13.         cout<<"B"<<endl;
  14.     if(b<0.7)
  15.         cout<<"A"<<endl;
  16.     else
  17.         cout<<"B"<<endl;
  18.     return 0;
  19. }
  20.  
  21.  
OUTPUT:

Expand|Select|Wrap|Line Numbers
  1. A
  2. B
  3.  
But I think first 'A' should not get printed, because a=0.7, how can if statement be true.
Can anyone please help me to find out my error.

Suyash

hello suyash,
when u store 0.7 in float and 0.7 in double they are stored differently, some kind of error comes then this two no. r differnt. due to memory area, thats why u r getting different answer.
Jun 18 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.