//CODE 1
#include<iostream>
using namespace std;
int main()
{
float a=0.7;
if(a==0.7)
cout<<"C++\n";
else
cout<<"C\n";
return(0);
}
When we execute the above code we get the o/p as :"C",
But excepted o/p is :"C++"
//CODE 2
#include<iostream>
using namespace std;
int main()
{
float a=0.7;
if(a==(float)0.7)
cout<<"C++\n";
else
cout<<"C\n";
return(0);
}
When we execute the above code we get the o/p as :"C++",
Bcoz: Computers use a binary (0's and 1's) system to store decimal numbers. This leads to some inaccuracy, since some decimal values can't be stored exactly in binary....0.7 is stored as the value 0.69999999.Since that value isn't 0.7, the condition fails..
The float function takes care of this problem -
float(0.7)
it rounds the value 0.69999999 to 0.7. Many decimal values are stored accurately in binary, for example 0.5, but many are not.
#include<iostream>
using namespace std;
int main()
{
float a=0.7;
if(a==0.7)
cout<<"C++\n";
else
cout<<"C\n";
return(0);
}
When we execute the above code we get the o/p as :"C",
But excepted o/p is :"C++"
//CODE 2
#include<iostream>
using namespace std;
int main()
{
float a=0.7;
if(a==(float)0.7)
cout<<"C++\n";
else
cout<<"C\n";
return(0);
}
When we execute the above code we get the o/p as :"C++",
Bcoz: Computers use a binary (0's and 1's) system to store decimal numbers. This leads to some inaccuracy, since some decimal values can't be stored exactly in binary....0.7 is stored as the value 0.69999999.Since that value isn't 0.7, the condition fails..
The float function takes care of this problem -
float(0.7)
it rounds the value 0.69999999 to 0.7. Many decimal values are stored accurately in binary, for example 0.5, but many are not.
No comments:
Post a Comment