18.10.2014 Views

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>SIMSCRIPT</strong> <strong>II.5</strong> <strong>Programming</strong> <strong>Language</strong><br />

and<br />

normally, mode is double<br />

2.3 Expression Modes<br />

Although statements that combine integer and real variables are allowed, a programmer should<br />

be aware of the way in which computations are carried out whenever using "mixed mode" expressions.<br />

When A and B represent two variables, constants, or sub-expressions, then:<br />

1. Arithmetic expressions of the form A op B, where A and B represent variables of specified<br />

mode, or constants of that mode, and op is any of the arithmetic operations +, -, and *, are<br />

integer if both A and B are integer, and real if either A or B is real.<br />

2. Expressions of the form A/B are always real (Two integer expressions can be divided to<br />

yield a truncated integer result by a library function called div.f. Library functions are<br />

described in paragraph 2.15).<br />

3. Expressions of the form A**B are always real.<br />

Compound expressions are evaluated from left to right as a sequence of simple expressions that are<br />

evaluated according to the above rules. In the following examples, if A, B, and C are integer,<br />

then:<br />

A/B + C<br />

A + B + C<br />

A**B + C<br />

is real<br />

is integer<br />

is real<br />

When an expression appears on the right-hand side of a let statement or in an add or subtract<br />

statement, and its mode differs from the mode of the variable on the left-hand side, the expression<br />

is converted to the mode of the variable before the value of the variable is changed. When the arithmetic<br />

expression constituents of logical expressions differ in mode, all integer expressions are<br />

converted to real before evaluating the logical expression as true or false.<br />

Conversions from integer to real are straightforward. An integer to real conversion takes<br />

the whole number that is the value of an integer variable and converts it to a real number with<br />

the same value; 25 becomes 25.0, -11 becomes -11.0, and so forth.<br />

Real to integer conversions are more complex. Obviously, any fractional part cannot be represented<br />

by the integer. Real values are rounded to whole numbers by adding +0.5 to the variable<br />

if its value is positive or -0.5 if it is negative, and truncating the result. If X is integer and e is<br />

some real-valued expression (formed according to the above rules), then:<br />

let X = e sets X = 0 if e = 0.2 since 0.2 + 0.5 = 0.7 → 0<br />

let X = e sets X = 1 if e = 1.4999 since 1.4999 + 0.5 = 1.9999 → 1<br />

let X = e sets X = 2 if e = 1.50000 since 1.50000 + 0.5 = 2.0000 → 2<br />

40

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!