HP C A.06.05 Reference Manual

Expressions and Operators
Assignment Operators (=, +=, -=, *=, /=, %=,<<=, >>=, &=, ^=, |=)
Chapter 5 91
This causes a loss of precision which could have a dramatic impact on your program. The
same truncation process occurs for negative values. After the assignment
j = -5.8;
the value of j is -5.
An equally serious situation occurs when the floating-point value cannot fit into an integer.
For example:
j = 999999999999.0
This causes an overflow condition which will produce unpredictable results. As a general rule,
it is a good idea to keep floating-point and integer values separate unless you have a good
reason for mixing them.
Double to Float Conversions As is the case with assigning floating-point values to
integer variables, there are also potential problems when assigning double values to float
variables. There are two potential problems: loss of precision and an overflow condition. In HP
Cadouble can represent approximately 16 decimal places, and a float can only represent 7
decimal places. If f is a float variable, and you make the assignment
f = 1.0123456789
the computer rounds the double constant value before assigning it to f. The value actually
assigned to f, therefore, will be 1.012346 (in double-to-float conversions, HP C always rounds
to the nearest float value). The following example shows rounding due to conversions.
/* Program name is "float_rounding". It shows how double values
can be rounded when they are assigned to a float. */
#include <stdio.h>
int main(void)
{
float f32;
double f64;
int i;
for (i = 1, f64 = 0; i < 1000; ++i)
f64 += 1.0/i;
f32 = f64;
printf("Value of f64: %1.7f\n", f64);
printf("Value of f32: %1.7f\n", f32);
}
The output is
Value of f64: 7.4844709
Value of f32: 7.4844708