还没有真正能够找到很多关于这个的信息。虽然当我用 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/