在 C++ 中,float 和 double 数据类型都用于表示浮点值。浮点数用于表示小数和指数值。例如:
// creating float type variables
float num1 = 3.0f;
float num2 = 3.5f;
float num3 = 3E-5f; // 3x10^-5
// creating double type variables
double num4 = 3.0;
double num5 = 3.5;
double num6 = 3E-5; // 3x10^-5
我们必须在 float 值末尾添加后缀 f 或 F。这是因为编译器默认将没有后缀的小数值解释为 double。
考虑以下代码。
float a = 5.6;
在这里,我们将一个 double 值赋给了一个 float 变量。
在这种情况下,编译器会自动将 5.6 转换为 float,然后再将其赋给变量 a。这可能会导致数据丢失。如需了解更多信息,请访问 C++ 类型转换。
float 和 double 的区别
| 浮点数 | 双精度浮点数 |
|---|---|
| 大小: 4 字节 | 大小: 8 字节 |
| 精度:一般为 7 位十进制数字精度 | 精度:一般为 15 位十进制数字精度 |
示例: 3.56f, 3e5f 等。 |
示例: 3.56, 3e5 等。 |
注意:除非有特定要求,否则请始终使用 double 而不是 float,因为在处理大数字时,float 变量可能容易引入错误。
示例 1:C++ float 和 double
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
// Creating a double type variable
double a = 3.912348239293;
// Creating a float type variable
float b = 3.912348239293f;
// Printing the two variables
cout << "Double Type Number = " << a << endl;
cout << "Float Type Number = " << b << endl;
return 0;
}
输出
Double Type Number = 3.91235 Float Type Number = 3.91235
注意:此示例使用的编译器 (MinGW 编译器) 允许 6 位数字。因此,我们的变量值被编译器四舍五入并截断为 6 位数字。
使用 setprecision() 指定小数点位数
我们可以使用 setprecision() 函数指定在 cout 中打印的小数位数。
此函数定义在 iomanip 头文件中,它代表输入/输出操纵。
示例 2:对浮点数使用 setprecision()
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
// Creating a double type variable
double a = 3.912348239293;
// Creating a float type variable
float b = 3.912348239293f;
// Setting the precision to 12 decimal places
cout << setprecision(13);
// Printing the two variables
cout << "Double Type Number = " << a << endl;
cout << "Float Type Number = " << b << endl;
return 0;
}
输出
Double Type Number = 3.912348239293 Float Type Number = 3.912348270416
从上面的示例可以看出,我们将精度指定到了 13 位。
cout << setprecision(13);
我们赋给变量的浮点值也包含 13 位数字。
然而,由于 float 的精度最高只有 7 位,当超过其精度时,它会显示垃圾值。
我们的 double 变量显示了正确的数字,因为它具有 15 位的精度,而数字本身包含 13 位。
作为替代,我们可以在打印时为不同的变量指定不同的精度。
示例 3:为不同变量指定不同精度
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
// Creating a double type variable
double a = 3.912348239293;
// Creating a float type variable
float b = 3.912348239293f;
// Setting precision to 11 for double
cout << "Double Type Number = " << setprecision(11) << a << endl;
// Setting precision to 7 for float
cout << "Float Type Number = " << setprecision(7) << b << endl;
return 0;
}
输出
Double Type Number = 3.9123482393 Float Type Number = 3.912348
从上面的程序可以看出,我们为 float 和 double 设置了两个不同的精度值。
在这两种情况下,精度都小于数字的实际位数。因此,最后一位被四舍五入,其余的被截断。
注意:如果我们指定的精度大于数据类型本身的精度 (float 为 7 位,double 为 15 位),那么编译器将在超过精度限制后给出垃圾值,正如在示例 2 中 float 输出所示。
处理指数数
如上所述,float 和 double 也可用于表示指数数。例如:
// ex = 325 X (10 ^ 25)
double ex = 325E25;
C++ 以一种称为科学计数法的格式输出指数数和非常大的数字。变量 ex 由于是一个非常大的数字,默认情况下将以这种格式输出。
为了强制 C++ 以 scientific 格式显示我们的浮点数,无论数字大小如何,我们会在 cout 中使用格式说明符 scientific。
double num = 3.25;
// ex = 325 X (10 ^ 25)
double ex = 325E25;
// using scientific format
cout << scientific << num;
cout << scientific << ex;
除此之外,还有一个格式说明符称为 fixed,它以十进制格式显示浮点数。
这类似于仅使用 cout 而不使用 setprecision() 来显示浮点数,不同之处在于 fixed 显示最多 6 位小数。
另一方面,仅使用 cout 会根据特定的编译器显示数字 (在MinGW 编译器的情况下,总共显示 6 位数字,包括小数点前的数字)。
示例 4:Fixed 和 Scientific 格式
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
// Creating a decimal double type variable
double a = 3.912348239293;
// Creating an exponential double type variable
double ex1 = 325e+2;
// Creating a float type variable
float b = 3.912348239293f;
// Creating an exponential float type variable
float ex2 = 325e+2f;
// Displaying output with fixed
cout << "Displaying Output With fixed:" << endl;
cout << "Double Type Number 1 = " << fixed << a << endl;
cout << "Double Type Number 2 = " << fixed << ex1 << endl;
cout << "Float Type Number 1 = " << fixed << b << endl;
cout << "Float Type Number 2 = " << fixed << ex2 << endl;
// Displaying output with scientific
cout << "\nDisplaying Output With scientific:" << endl;
cout << "Double Type Number 1 = " << scientific << a << endl;
cout << "Double Type Number 2 = " << scientific << ex1 << endl;
cout << "Float Type Number 1 = " << scientific << b << endl;
cout << "Float Type Number 2 = " << scientific << ex2 << endl;
return 0;
}
输出
Displaying Output With fixed: Double Type Number 1 = 3.912348 Double Type Number 2 = 32500.000000 Float Type Number 1 = 3.912348 Float Type Number 2 = 32500.000000 Displaying Output With scientific: Double Type Number 1 = 3.912348e+000 Double Type Number 2 = 3.250000e+004 Float Type Number 1 = 3.912348e+000 Float Type Number 2 = 3.250000e+004
long double
除了 float 和 double 之外,还有另一种可以存储浮点数的 C++ 数据类型。这就是 long double。
它通常占用 12 字节的空间 (取决于使用的计算机系统),并且其精度至少与 double 相同,但大多数情况下,它大于 double 的精度。
long double 值应以 L 结尾。例如:
// declaring a long double variable
long double num_ldb = 2.569L;
注意:C++ 支持的浮点数据类型有 float、double 和 long double。没有 long float。