18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

48 <strong>Ada</strong> <strong>in</strong>troduction: Part 2<br />

4.11.5 Constra<strong>in</strong>ed and unconstra<strong>in</strong>ed types<br />

In <strong>Ada</strong> there are no named types only subtypes. The Integer and Float types are derived from<br />

Root_Integer and Root_Float respectively. Range checks only apply to constra<strong>in</strong>ed subtypes, but<br />

overflow checks always apply. For example, us<strong>in</strong>g the declaration of Exam_Mark:<br />

type Exam_Mark is new Integer range 0 .. 100;<br />

the follow<strong>in</strong>g properties hold.<br />

Declaration Instance is Commentary<br />

Exam_Mark Constra<strong>in</strong>ed Constra<strong>in</strong>ed to the range 0 .. 100.<br />

Exam_Mark'Base Unconstra<strong>in</strong>ed<br />

No range checks applied to assignment of<br />

this variable. An implementor may allow<br />

this to have a range greater than the base<br />

range of the root type.<br />

Declaration Instance is Commentary<br />

Integer Constra<strong>in</strong>ed Constra<strong>in</strong>ed to the base range of Integer,<br />

which is implementation dependent.<br />

Integer'Base Unconstra<strong>in</strong>ed No range checks apply; may have a range greater<br />

than Integer.<br />

Note:<br />

Regardless of whether an item is constra<strong>in</strong>ed or unconstra<strong>in</strong>ed, overflow checks will always apply.<br />

Thus, the result obta<strong>in</strong>ed will always be mathematically correct.<br />

Take note of the difference between Integer and Integer'Base. Instances of the type Integer<br />

are constra<strong>in</strong>ed to the base range of the type, whilst <strong>in</strong>stances of Integer'Base are not.<br />

4.11.6 Implementation optimizations<br />

An <strong>Ada</strong> compiler is allowed to represent an <strong>in</strong>stance of a base type to a greater precision than is necessary. For<br />

example, with the follow<strong>in</strong>g declarations:<br />

type Exam_Mark is new Integer range 0 .. 100;<br />

type Temporary is Exam_mark'Base;<br />

English : Exam_Mark;<br />

Total : Temporary<br />

the variable Total may be implemented to hold numbers of a greater range than is allowed by an Integer<br />

declaration. This is to allow compiler writers the opportunity to perform optimizations such as hold<strong>in</strong>g a variable<br />

or <strong>in</strong>termediate result <strong>in</strong> a CPU register which may have a greater precision than the range of normal Integer<br />

values. Of course, overflow checks will be performed at all times, so the mathematical result is always correct.<br />

The danger is that a program which compiles and runs successfully us<strong>in</strong>g a particular compiler on a mach<strong>in</strong>e<br />

may fail to run successfully when compiled with a different compiler on the same mach<strong>in</strong>e, even though both<br />

compilers have the same range for an Integer.<br />

© M A Smith - May not be reproduced without permission

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

Saved successfully!

Ooh no, something went wrong!