[英]Floating point variables in c++
當八位數溢出后在c ++中使用float或double時,如何解決?
這是我的代碼:
#include <iostream.h>
#include <conio.h>
void main() {
double x;
cout<<"double : ";
cin>>x;
cout<<endl<<x;
getch();
}
當cin = 123456789時,這是我的提示:123457e.08。
使用點:double x = 2398479238749234.0
如果聲明浮點數,則可以在末尾鍵入f
,如下所示:
float var = 123456789.0f;
一個簡單而有趣的演示:
要查看f
的重要性,請嘗試以下代碼:
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;
輸出:
f1等於1.3f
f2不等於1.3
參見ideone上的演示: http : //www.ideone.com/QvzEp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.