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

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

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

Note:<br />

In reality the float<strong>in</strong>g po<strong>in</strong>t number will be held <strong>in</strong> b<strong>in</strong>ary.<br />

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

The ma<strong>in</strong> consequence of us<strong>in</strong>g a float<strong>in</strong>g po<strong>in</strong>t number is that numbers are held to an approximation of their<br />

true value. Calculations us<strong>in</strong>g float<strong>in</strong>g po<strong>in</strong>t numbers will usually only give an approximation to the true answer.<br />

However, <strong>in</strong> many cases this approximation will not cause any problems. An area where this approximation will<br />

cause problems is when the value represents a monetary amount.<br />

The attributes 'First, 'Last and 'Size may also be applied to objects of type Float. In addition the<br />

attribute 'Digits returns the precision <strong>in</strong> decimal digits of a number stored <strong>in</strong> a Float object. For example, the<br />

follow<strong>in</strong>g program:<br />

with <strong>Ada</strong>.Text_Io;<br />

use <strong>Ada</strong>.Text_Io;<br />

procedure Ma<strong>in</strong> is<br />

beg<strong>in</strong><br />

Put("Smallest Float ");<br />

Put( Float'Image( Float'First ) ); New_L<strong>in</strong>e;<br />

Put("Largest Float ");<br />

Put( Float'Image( Float'Last ) ); New_L<strong>in</strong>e;<br />

Put("Float (bits) ");<br />

Put( Integer'Image( Float'Size ) ); New_L<strong>in</strong>e;<br />

Put("Float (digits) ");<br />

Put( Integer'Image( Float'digits ) ); New_L<strong>in</strong>e;<br />

end Ma<strong>in</strong>;<br />

Note: Float'Image delivers a str<strong>in</strong>g represent<strong>in</strong>g a float<strong>in</strong>g po<strong>in</strong>t number <strong>in</strong> scientific notation.<br />

when compiled and run on two different mach<strong>in</strong>es would produce:<br />

Mach<strong>in</strong>e us<strong>in</strong>g a 32 bit word size<br />

Smallest Float -3.40282E+038<br />

Largest Float 3.40282E+038<br />

Float (bits) 32<br />

Float (digits) 6<br />

Mach<strong>in</strong>e us<strong>in</strong>g a 64 bit word size<br />

Smallest Float -1.79769313486232E+308<br />

Largest Float 1.79769313486232E+308<br />

Float (bits) 64<br />

Float (digits) 15<br />

4.2.1 Other Integer and Float data types<br />

Some implementations of <strong>Ada</strong> may provide data types that offer a greater precision than the <strong>in</strong>-built types of<br />

Integer and Float. If these are provided they will be called Long_Integer, Long_Float,<br />

Long_Long_Integer etc.<br />

4.3 New data types<br />

Us<strong>in</strong>g an object of type Integer to hold numeric values may be a useful approach, but it does not lead to a<br />

program that is mach<strong>in</strong>e <strong>in</strong>dependent. For example, dur<strong>in</strong>g its execution a program could create values which<br />

were not conta<strong>in</strong>able <strong>in</strong> a particular mach<strong>in</strong>e's Integer object. If this happened, then the program would fail<br />

with a run-time error of ‘Constra<strong>in</strong>t_Error’.<br />

<strong>Ada</strong> provides an elegant solution to this problem. It allows a user to def<strong>in</strong>e a new data type, which has a<br />

specific range of values. For example, the follow<strong>in</strong>g declaration def<strong>in</strong>es a new data type Distance which will<br />

hold the distance between two places:<br />

type Distance is range 0 .. 250_000;<br />

Note:<br />

If the compiler cannot provide an object which can hold such a range, a compile-time error message<br />

will be generated.<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!