Floating point variables in c++
When i use float or double in c++ after eight digit is getting overflow, how fix it?
This is my code :
#include <iostream.h>
#include <coni开发者_运维问答o.h>
void main() {
double x;
cout<<"double : ";
cin>>x;
cout<<endl<<x;
getch();
}
When cin = 123456789 , this is my cout : 123457e.08.
Use dot: double x = 2398479238749234.0
If you declare a float you can type f
at the end like this:
float var = 123456789.0f;
A simple yet interesting demonstration:
To see the importance of f
try this code:
float f1 = 1.3f;
//test f1
if ( f1 == 1.3f )
std::cout<<"f1 is equal to 1.3f"<<std::endl;
else
std::cout<<"f1 is not equal to 1.3f"<<std::endl;
float f2 = 1.3;
//test f2
if ( f2 == 1.3 )
std::cout<<"f2 is equal to 1.3"<<std::endl;
else
std::cout<<"f2 is not equal to 1.3"<<std::endl;
Output:
f1 is equal to 1.3f
f2 is not equal to 1.3
See demonstration at ideone : http://www.ideone.com/QvzEp
精彩评论