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.
Implicit conversions of
decimal floating-point types (IBM extension)
The
compiler has the following decimal floating-point types:
- _Decimal32
- _Decimal64
- _Decimal128
The following implicit conversions are always supported:
- Implicit conversions between decimal floating-point types:
- _Decimal32 to _Decimal64
- _Decimal32 to _Decimal128
- _Decimal64 to _Decimal32
- _Decimal64 to _Decimal128
- _Decimal128 to _Decimal32
- _Decimal128 to _Decimal64
- Implicit conversions between decimal floating-point types and
the following integer types:
- char, signed char, unsigned
char
- short, unsigned short
- int, unsigned int
- long int, unsigned long int
- long long int, unsigned long long int
- Implicit conversions between decimal floating-point types and
Boolean types bool or _Bool.
Implicit conversions between decimal floating-point types
and the following generic floating-point types are supported conditionally.
It is supported through assignment operation using the simple assignment
operator =, initialization, function argument passing and function
return statements.
The following examples demonstrate the implicit conversion
from a generic floating-point type to a decimal floating-point type.
In this example, variable
f1 is implicitly converted
from type
float to type
_Decimal32 in
the initialization.
float f1;
_Decimal32 d1 = f1;
float f1;
_Decimal32 d1(f1);
Restriction: You cannot mix decimal floating-point
types with generic floating-point types or complex floating-point
types in arithmetic expressions unless you use explicit conversions.
Here is an example:
_Decimal32 d1;
float f1;
float f2 = f1 + d1; // Incorrect
float f3 = f1 + (float)d1; // Correct
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.