The standard rule for converting between real floating
point types (binary to binary, decimal to decimal
and decimal to binary) is as follows:
If the value being converted can be represented exactly
in the new type, it is unchanged. If the value being converted is
in the range of values that can be represented but cannot be represented
exactly, the result is rounded, according to the current compile-time
or runtime rounding mode in effect. If the value being converted is
outside the range of values that can be represented, the result is
dependent on the rounding mode.
- Integer to floating point (binary or decimal)
- If the value being converted can be represented exactly in the
new type, it is unchanged. If the value being converted is in the
range of values that can be represented but cannot be represented
exactly, the result is correctly rounded. If the value being converted
is outside the range of values that can be represented, the result
is quiet NaN.
- Floating point (binary or decimal) to
integer
- The fractional part is discarded (i.e., the value is truncated
toward zero). If the value of the integral part cannot be represented
by the integer type, the result is one of the following:
- If the integer type is unsigned, the result is the largest representable
number if the floating point number is positive, or 0 otherwise.
- If the integer type is signed, the result is the most negative
or positive representable number according to the sign of the floating
point number.
Complex conversions
- Complex to complex
- If the types are identical, there is no change. If the types are
of a different size, and the value can be represented by the new type,
the value is not changed; if the value cannot be represented by the
new type, both real and imaginary parts are converted according to
the standard conversion rule given above.
- Complex to real (binary)
- The imaginary part of the complex value is discarded. If necessary,
the value of the real part is converted according to the standard
conversion rule given above.
- Complex to real (decimal)
- The imaginary part of the complex value is discarded. The value
of the real part is converted from binary to decimal floating point,
according to the standard conversion rule given above.
- Real (binary) to complex
- The source value is used as the real part of the complex value,
and converted, if necessary, according to the standard conversion
rule given above. The value of the imaginary part is zero.
- Real (decimal) to complex
- The source value is converted from decimal to binary floating
point, according to the standard conversion rule given above, and
used as the real part of the complex value. The value of the imaginary
part is zero.