c++我可以用double初始化一个float类型的变量吗?

标签 c++ types floating-point initialization double

还没有真正能够找到很多关于这个的信息。虽然当我用 double 初始化类型为 float 的变量时编译器似乎没有提示,例如

double a = 1.0;
float b = a;

它工作正常,但我仍然很好奇这样做是否可以,和/或是否安全?

(我在运行 windows 的 32 位系统上)

最佳答案

4.8 Floating point conversions

1 A prvalue of floating point type can be converted to a prvalue of another floating point type. If the source value can be exactly represented in the destination type, the result of the conversion is that exact representation. If the source value is between two adjacent destination values, the result of the conversion is an implementation-defined choice of either of those values. Otherwise, the behavior is undefined.

关于c++我可以用double初始化一个float类型的变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40792246/

相关文章:

c++ - const最佳匹配函数与其他函数之间的歧义

Haskell:代数数据类型,其类型变量需要是类型类的实例

c# - 引用类中的 Self's Type

algorithm - 计算浮点值与整数相乘以产生小于 1 的最大数

C++ : Data Structure withe fast searching and less memory requirements

c++ - 如何使用线性插值避免 "trap"?

c++ - Qt 数据类型选择

c - Objective-C中的地板问题

c - 如何修复用于分隔整数和小数部分的代码

c++ - 释放空指针会导致以下弹出消息