Next: Integer Division
Up: Mixing Objects of Different
Previous: Mixed Numeric Type Expressions
When the whole RHS expression of a mixed type assignment statement has been
evaluated it has a type, this type must then be converted to fit in with
the LHS. This conversion could be either a promotion or a relegation.
For example,
- LHS INTEGER; RHS REAL or DOUBLE PRECISION -- the RHS needs relegating
to be
an INTEGER value. The right hand side is evaluated and
then the value is truncated (all the decimal places lopped off) then
assigned to the LHS.
- LHS REAL or DOUBLE PRECISION; RHS INTEGER -- the INTEGER needs
promoting to become a REAL. The right hand side expression is
simply stored (approximately) in the LHS.
For example, as real values are stored approximately,
REAL a,b; INTEGER i
a = 3.0; b = 4.0
i = b - a
j = -0.9
a = j
-
the result of b - a would be close to 1.0 (it could be
1.0000001 or it could be 0.999999999), thus, due to truncation
i could contain either 0 or 1.
- j would be
truncated to 0; integers are always formed by truncating towards
zero.
- a would be set to (about) 0.000.
As long as care is taken, simple assignment should be trouble free.
Next: Integer Division
Up: Mixing Objects of Different
Previous: Mixed Numeric Type Expressions
Adam Marshall ©University of Liverpool, 1996
Fri Dec 6 15:03:35 GMT 1996Not for commercial use.