以下哪些是浮点数的正确写法:12.3, 12.3e+2, 23.4e-2, -334.4, 20.5, 39, 40
正确的浮点数:
12.3
12.3e+2
23.4e-2
-334.4
20.5
不是浮点数的值:
39(这是一个整数)
40(这是一个整数)
原因解释
浮点数的特点:
必须包含小数点(如 12.3)或 科学计数法(如 12.3e+2)。
可以是正数或负数(如 -334.4)。
科学计数法中的 e 表示 10 的幂次方(如 12.3e+2 表示 12.3 × 10^2)。
整数不是浮点数:
39 和 40 是整数,因为它们没有小数点或科学计数法。
可以通过以下代码验证哪些是浮点数:
#include <iostream>
#include <typeinfo>
using namespace std;
int main() {
cout << "12.3 is of type: " << typeid(12.3).name() << endl;
cout << "12.3e+2 is of type: " << typeid(12.3e+2).name() << endl;
cout << "23.4e-2 is of type: " << typeid(23.4e-2).name() << endl;
cout << "-334.4 is of type: " << typeid(-334.4).name() << endl;
cout << "20.5 is of type: " << typeid(20.5).name() << endl;
cout << "39 is of type: " << typeid(39).name() << endl;
cout << "40 is of type: " << typeid(40).name() << endl;
return 0;
}