实型是用来定义小数的
分为两类
1、单精度 float,占4字节
2、双精度 double,占8字节
注:在用 float 定义小数时,后面要加“ f ”,不然会默认为双精度(“ double ”)
科学计数法
" e "后为正数( n )表示 10 ^ n,为负数( - n )表示 10 ^ -n
例如:
3e2 = 3 * 10 ^ 2
3e-2 = 3 * 10 ^ -2
下面是一个实例
#include<iostream>
using namespace std;
int main() {
//1、单精度
//2、双精度
//默认情况下,输出一个小数会显示6位有效数字
float f1 = 3.14f;
cout << "f1 = " << f1 << endl;
double d1 = 3.14;
cout << "d1 = " << d1 << endl;
//统计float和double占用内存空间
cout << "float 占用的内存空间为: " << sizeof(float) << endl; // 4字节
cout << "double 占用的内存空间为: " << sizeof(double) << endl; // 8字节
//科学计数法
float f2 = 3e2; //3 * 10 ^ 2
cout << "f2 = " << f2 << endl;
float f3 = 3e-2; //3 * 10 ^ -2
cout << "f3 = " << f3 << endl;
system("pause");
return 0;
}
输出结果为