Appendix B: Arithmetic types

This appendix deals witth conversion, reading and writing of arithmetic values. It is intended to be read in conjunction with appendix A.

Implicit conversion

The circumstances in which implicit conversion occurs were outlined in chapter 3. The rules for expression evaluation were described formally in appendix A. This section explains the rules for rounding.

Short integer to integer conversion is always exact.

Integer to short integer conversion resulting from assignment of an integer value to a short integer location or passing an integer value as a short integer parameter is exact where it is possible. Where the integer's value lies outside the range of short integer values for that system, the attempted conversion will cause a runtime error. All value ranges are system dependent.

Real to long real conversion should involve no loss of precision, since long reals are required to allow greater or equal precision. Some systems restrict the range of long real values to obtain greater precision, compared to reals. In these systems it is possible to cause a runtime error by attempting to convert a real to a long real.

Long real to real conversion may result in loss of precision. If the value range of long real exceeds that of real for the system, a runtime error will result if the long real is outside the real range.

Integer to real or long real conversion is exact within some system defined range, which must include zero. Outside that range it may be non-exact. In general no range errors should be possible.

Real or long real to integer or short integer may cause a runtime error if the is defined as equivalent to adding 0.5 to the value and taking the integer part of the result. Thus 3.5 becomes 4, while -3.5 becomes -3.

Precision

All non-integer arithmetic values are stored with a finite precision. the effect of certain mathematical operations is dependent on the sensitivity of the expression to rounding errors.

The system procedures AddEpsilon and SubEpsilon (see chapter 20) are provided to help with advanced numerical programming. They determine the sensitivity of real or long real values to addition and subtraction respectively.

Floating point representation

SIMULA recognises the following real number forms, both within source programs and in input from ImageFiles.
   <sign?><spaces?><integer_part><exponent?>
      or
  < sign?><spaces?><integer_part?><decimal_mark><fractional_part><exponent?>
      or
  < sign?><exponent>
A plus or minus sign is optional. A plus has no effect; a minus denotes a negative number.

The integer part is optional when a decimal mark and fractional part are given. It consists of a sequence of digits. When no fractional part is given an integer part followed by an exponent is regarded as a real or long real constant.

There must be a decimal mark if there is a fractional part. The system will normally expect a full stop (period), but this can be reset using the system procedure DecimalMark (see chapter 20). This also allows the current mark to be checked.

The fractional part is another, non-empty, string of digits.

The exponent is optional where at least a fractional part is given, but may be used on its own. It consists of the lowten symbol followed by an integer value. It specifies a power of ten by which the value of the rest of the number is to be multiplied or is itself, combined with a sign part if present, the value of the number.

The lowten character is normally ampersand (&) for real numbers and double ampersand (&&) for long reals. Note that this allows the precision of the number to be specified for constants. The ampersand is replaced by any character passed to the system procedure LowTen (see chapter 20). This procedure also returns the current character.

Valid real constants include:

   2.67
   0.54
    .54
   - 12.68&-6
   &&12

Floating point output

OutReal and PutReal generate texts in floating point form, using the fullest variant of the above.

PutReal has two parameters. The first is the real or long real value to be represented, the other controls the precise format.

Real values are printed with a single lowten character, long reals with a double.

The second, integer parameter of PutReal has the following effects:

Thus the following calls produce the output given:

   PutReal(34.56,0)   ->   &+01
   PutReal(34.56,1)   ->   3&+01
   PutReal(34.56,2)   ->   3.5&+01
Systems may vary in the number of digits output for the exponent. You should consult the documentation for the system you are using.

OutReal takes a third parameter, which is an integer used to specify the total width allocated for the number to be output, as explained in chapter 8.