Conversions from Floating-Point Types

 

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

A float value converted to a double or long double, or a double converted to a long double, undergoes no change in value. 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.

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. The decimal portion of the floating value is discarded in the conversion to a long. If the result is still too large to fit into a long, the result of the conversion is undefined.

Microsoft Specific

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. Note that the Microsoft C compiler maps long double to type double.

END Microsoft Specific

The following table summarizes conversions from floating types.

Conversions from Floating-Point Types

FromToMethod
floatcharConvert to long; convert long to char
floatshortConvert to long; convert long to short
floatlongTruncate at decimal point. If result is too large to be represented as long, result is undefined.
floatunsigned shortConvert to long; convert long to unsigned short
floatunsigned longConvert to long; convert long to unsigned long
floatdoubleChange internal representation
floatlong doubleChange internal representation
doublecharConvert to float; convert float to char
doubleshortConvert to float; convert float to short
doublelongTruncate at decimal point. If result is too large to be represented as long, result is undefined.
doubleunsigned shortConvert to long; convert long to unsigned short
doubleunsigned longConvert to long; convert long to unsigned long
doublefloatRepresent as a float. If double value cannot be represented exactly as float, loss of precision occurs. If value is too large to be represented as float, the result is undefined.
long doublecharConvert to float; convert float to char
long doubleshortConvert to float; convert float to short
long doublelongTruncate at decimal point. If result is too large to be represented as long, result is undefined.
long doubleunsigned shortConvert to long; convert long to unsigned short
long doubleunsigned longConvert to long; convert long to unsigned long
long doublefloatRepresent as a float. If double value cannot be represented exactly as float, loss of precision occurs. If value is too large to be represented as float, the result is undefined.
long doubledoubleThe long double value is treated as double.

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.

Assignment Conversions

Show: