C++初学:以下哪些是浮点数的正确写法: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
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;
}

输出示例

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值