从浮点类型的转换Conversions from Floating-Point Types

转换为 double 或 long double 的 float 值,或转换为 long double 的 double 值不会发生更改。A float value converted to a double or long double, or a double converted to a long double, undergoes no change in value. 如果可能,请准确表示转换为 float 值的 double 值。A double value converted to a float value is represented exactly, if possible. 如果无法准确表示该值,则可能会丢失精度。Precision may be lost if the value cannot be represented exactly. 如果结果超出范围,则该行为是不确定的。If the result is out of range, the behavior is undefined. 有关浮点类型的范围,请参阅浮点常量的限制See Limits on Floating-Point Constants for the range of floating-point types.

通过以下方式将浮点值转换为整数值:先将其浮点值转换为 long,然后从 long 值转换为特定整数值。A floating value is converted to an integral value by first converting to a long, then from the long value to the specific integral value. 浮点值的小数部分在转换为 long 的过程中将被丢弃。The decimal portion of the floating value is discarded in the conversion to a long. 如果结果仍过大而无法放入 long,则转换的结果是不确定的。If the result is still too large to fit into a long, the result of the conversion is undefined.

Microsoft 专用Microsoft Specific

在将 double 或 long double 浮点数转换为更小的浮点数时,如果发生下溢,则浮点变量的值将向零截断。When converting a double or long double floating-point number to a smaller floating-point number, the value of the floating-point variable is truncated toward zero when an underflow occurs. 溢出会导致出现运行时错误。An overflow causes a run-time error. 请注意,Microsoft C 编译器会将 long double 映射到类型 double。Note that the Microsoft C compiler maps long double to type double.

结束 Microsoft 专用END Microsoft Specific

下表汇总了来自浮点型的转换。The following table summarizes conversions from floating types.

从浮点类型的转换Conversions from Floating-Point Types

FromFrom To 方法Method
floatfloat char 转换为 long;将 long 转换为 charConvert to long; convert long to char
floatfloat shortshort 转换为 long;将 long 转换为 shortConvert to long; convert long to short
floatfloat longlong 在小数点处截断。Truncate at decimal point. 如果结果过大而无法表示为 long,则结果是不确定的。If result is too large to be represented as long, result is undefined.
floatfloat unsigned shortunsigned short 转换为 long;将 long 转换为 unsigned shortConvert to long; convert long to unsigned short
floatfloat unsigned long 转换为 long;将 long 转换为 unsigned longConvert to long; convert long to unsigned long
floatfloat 双精度double 更改内部表示形式Change internal representation
floatfloat long double 更改内部表示形式Change internal representation
双精度double char 转换为 float;将 float 转换为 charConvert to float; convert float to char
双精度double shortshort 转换为 float;将 float 转换为 shortConvert to float; convert float to short
双精度double longlong 在小数点处截断。Truncate at decimal point. 如果结果过大而无法表示为 long,则结果是不确定的。If result is too large to be represented as long, result is undefined.
双精度double unsigned shortunsigned short 转换为 long;将 long 转换为 unsigned shortConvert to long; convert long to unsigned short
双精度double unsigned long 转换为 long;将 long 转换为 unsigned longConvert to long; convert long to unsigned long
双精度double floatfloat 表示为 float。Represent as a float. 如果 double 值不能正确表示为 float,则会丢失精度。If double value cannot be represented exactly as float, loss of precision occurs. 如果值太大而无法表示为 float,则结果是不确定的。If value is too large to be represented as float, the result is undefined.
long double char 转换为 float;将 float 转换为 charConvert to float; convert float to char
long double shortshort 转换为 float;将 float 转换为 shortConvert to float; convert float to short
long double longlong 在小数点处截断。Truncate at decimal point. 如果结果过大而无法表示为 long,则结果是不确定的。If result is too large to be represented as long, result is undefined.
long double unsigned shortunsigned short 转换为 long;将 long 转换为 unsigned shortConvert to long; convert long to unsigned short
long double unsigned long 转换为 long;将 long 转换为 unsigned longConvert to long; convert long to unsigned long
long double floatfloat 表示为 float。Represent as a float. 如果 double 值不能正确表示为 float,则会丢失精度。If double value cannot be represented exactly as float, loss of precision occurs. 如果值太大而无法表示为 float,则结果是不确定的。If value is too large to be represented as float, the result is undefined.
long double 双精度double long double 值被视为 double。The long double value is treated as double.

如果要转换的值大于最大正 long 值,则从 float、double 或 long double 值到 unsigned long 的转换是不准确的。Conversions from float, double, or long double values to unsigned long are not accurate if the value being converted is larger than the maximum positive long value.

另请参阅See Also

赋值转换Assignment Conversions