简体   繁体   中英

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 <conio.h> 
void main() { 
  double x; 
  cout<<"double : "; 
  cin>>x; 
  cout<<endl<<x; 
  getch(); 
}

When cin = 123456789 , this is my cout : 123457e.08.

使用点: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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM