HP C A.06.05 Reference Manual
Expressions and Operators
Cast Operator
Chapter 5 101
Casting Floating-Point Values to Integers Casting floating-point values to integers may
produce useless values if an overflow condition occurs. The conversion is made simply by
truncating the fractional part of the number. For example, the floating-point value 3.712 is
converted to the integer 3, and the floating-point value -504.2 is converted to -504.
Here are some more examples:
float f = 3.700, f2 = -502.2, f3 = 7.35e9;
(int)f => 3
(unsigned int)f => 3
(char)f => 3
(int)f2 => -502 in decimal fffffe0a in hex
(unsigned int)f2 => 4294966794 in decimal or fffffe0a in hex
(char)f2 => 10 in decimal 0a in hex
(int)f3 => run-time error
(unsigned int)f3 => run-time error
(char)f3 => run-time error
NOTE Converting a large float to a char produces unpredictable results if the
rounded value cannot fit in one byte. If the value cannot fit in four bytes, the
run-time system issues an overflow error.
Casting Enumerated Values to Integers
When you cast an enumerated expression, the conversion is performed in two steps. First, the
enumerated value is converted to an int, and then the int is converted to the final target
data type. The sign is preserved during these conversions.
Casting Double to Float and Vice Versa
When you cast a float to a double, the system extends the number's precision without
changing its true value. However, when you cast a double to a float, the system shrinks the
number's precision, and this shrinking may change the number's value because of rounding.
The rounding generally occurs on the sixth or seventh decimal digit. Also, when you cast down
from double to float, you run the risk of causing a run-time overflow error caused by a
double that is too big or too small to fit in a float.
Casting Pointers to Pointers
You may cast a pointer of one type to a pointer to any other type. For example: