是否应该避免这种从 int 到双精度的静态转换?
Should this static cast from int to double be avoided?
考虑:
int total = 14; // something for which double does not make sense
int nPeople = 5;
double perCapita = static_cast<double>(total) / nPeople; // should this cast be avoided?
在这种情况下应该避免铸造吗?写1.0 * total / nPeople
好还是别的好?
在这种情况下应该避免投射吗?写1.0更好吗 * 总计/n人还是别的什么?
乘以 1.0 并不能明确意图。此处的显式强制转换要清晰得多,并且是执行此操作的实际接受方式。如果我们有某种可以显式调用的模板函数(一个不叫std::div
- 因为这是一种不同的划分(,那就太好了——但唉,我们没有,直到那时 - 只是static_cast<double>
!
相关文章:
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 正在将csv文件读取为双精度矢量
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 如何在C++中的同一函数中使用字符串和双精度
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 检查是否以特定精度给出双精度
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- C 字符串返回字符串的整数/双精度/长整型值
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 如何使双精度值的 C++ 和 C# 中的结果相同
- 使用浮点数和双精度数的非常小数字的数学
- 使用 Xcode 将双精度存储在数组C++中
- 是否应该避免这种从 int 到双精度的静态转换?
- 如何静态检查模板的类型 T 是否为 std::vector<U>,其中 U 是浮点数、双精度数或积分
- 函数中指向静态双精度的指针数组C++
- C++ 为什么我可以初始化静态常量字符,但不能初始化类定义中的静态常量双精度
- 双精度型静态const数组的静态const数组
- c++将静态二维双精度数组转换为双精度**
- 推回静态向量<对<字符串,双精度[9]> > myVector;
- 与 complex.h 一起使用时对静态常量双精度的未定义引用