home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog1 / chap06.txt < prev    next >
Encoding:
Text File  |  1991-07-01  |  29.8 KB  |  705 lines

  1.  
  2.  
  3.                                                         Chapter 6
  4.                                           ADDITIONAL SCALAR TYPES
  5.  
  6.  
  7. OUR OWN TYPES
  8. _________________________________________________________________
  9.  
  10. Most of the example programs in this tutorial have used the
  11. predefined type INTEGER for illustrating various concepts, and it
  12. is an excellent choice due to its versatility.  There are other
  13. types available because they are part of the Ada definition, and
  14. we can define our own types for special purposes.  This chapter
  15. will illustrate to you some of the reasons for doing so.
  16.  
  17. A complete description of types will be given in the next chapter
  18. but first we will learn how to use some of them.  We are caught in
  19. a dilemma like the proverbial, "which came first, the chicken or
  20. the egg?", and must select which to define first.  We have chosen
  21. to illustrate usage in this chapter, and then give the details of
  22. type definition in the next chapter.
  23.  
  24.  
  25.  
  26. A FEW integer CLASS TYPES
  27. _________________________________________________________________
  28.  
  29. Examine the file named ALLINT.ADA for some       ================
  30. examples of how and why we can use our own type     ALLINT.ADA
  31. definitions.  The three types illustrated in     ================
  32. lines 10 through 12 are available with all Ada
  33. compilers because they are so versatile and
  34. useful, and because they are required by the Ada programming
  35. language.  As we have said before, the type INTEGER defines a
  36. variable that can have any whole value between -32768 to 32767 on
  37. most microcomputers and some minicomputers too.  It should be
  38. pointed out that most minicomputers use a much larger range as
  39. standard.  The type NATURAL defines a variable from 0 to 32767, and
  40. the type POSITIVE covers the range from 1 to 32767 on most
  41. microcomputers.
  42.  
  43. Consider the word "most" in the last paragraph, and think about the
  44. problems you could have if you wrote a program that depended on a
  45. particular variable covering the listed range, and tried to move
  46. the program to a different machine which used a different range.
  47. You could be faced with a large rewrite problem in order to get
  48. the program to work on the new computer.
  49.  
  50.  
  51.  
  52. HOW CAN WE HELP SOLVE PORTABILITY PROBLEMS
  53. _________________________________________________________________
  54.  
  55. Suppose we defined our type to cover a certain range, such as
  56. illustrated in line 14 of the example program, and moved the
  57.  
  58.                                                          Page 6-1
  59.  
  60.                               Chapter 6 - Additional Scalar Types
  61.  
  62. program to another computer.  According to the definition of Ada,
  63. the new compiler would be obligated to create a type for us that
  64. would cover the given range, or give us a compile time error
  65. telling us that the hardware simply could not support the defined
  66. range.  In this case, due to the rather small range requested, any
  67. meaningful compiler and machine combination would be able to cover
  68. the defined range, and we would have a program that would run in
  69. spite of differences in the way the standard types were defined.
  70. Good programming practice, especially if the source code may need
  71. to be moved to other computers, would define all ranges explicitly
  72. and avoid the implementation defined limits built into the
  73. compiler.
  74.  
  75. Two new types are defined in lines 14 and 15, and the program uses
  76. some new attributes to illustrate the new types.  In lines 24 and
  77. 27, we use two attributes which we have used before, but in lines
  78. 31 and 34, we use two new attributes.  In order for the system to
  79. create a type which covers a range of -1000 to 24000, it must use
  80. a structure with enough binary bits to cover the given range.  The
  81. range is not composed of binary limits so the system will have to
  82. define enough bits to cover this range and a little more.  It will
  83. probably define some number of 8-bit bytes and the range covered
  84. by the full pattern, as defined, is called the base range.  The two
  85. new attributes give the limits of the base selected by the system.
  86. The base limits will probably be -32768 to 32767 if you are using
  87. a microcomputer, as you will see when you execute the program.
  88.  
  89.  
  90.  
  91. DO YOU HAVE A SMART COMPILER?
  92. _________________________________________________________________
  93.  
  94. The type illustrated here named MY_SHORT has defined limits of -12
  95. and 127, a relatively small range.  It is small enough that it can
  96. fit into a base range of -128 to 127, which could be stored in a
  97. single 8-bit byte.  If your compiler is smart enough to realize
  98. that, it could use a single 8-bit byte to store every variable of
  99. this type, and if you had a lot of these to store, it would save
  100. you a lot of memory.  You will probably find however, that most
  101. compilers will simply use the full INTEGER range for the base type
  102. of even this small number.
  103.  
  104. Four attributes of two different types are displayed on the monitor
  105. for your information.  You can see from the results of running this
  106. program exactly how your compiler stores these two types.
  107.  
  108.  
  109.  
  110. THE in AND not in OPERATORS
  111. _________________________________________________________________
  112.  
  113. We have a new operator to learn about now, the in operator
  114. illustrated in line 48.  If the variable Index, which has a current
  115. value of 345 due to initialization, is within the defined range of
  116.  
  117.                                                          Page 6-2
  118.  
  119.                               Chapter 6 - Additional Scalar Types
  120.  
  121. the subtype MY_SUBTYPE, a BOOLEAN type TRUE will be returned,
  122. otherwise a BOOLEAN type FALSE is returned.  This result can be
  123. assigned to a BOOLEAN variable or used for a boolean decision as
  124. shown.  In this case, the value of Index is not in the range of
  125. MY_SUBTYPE so a FALSE is returned and the message will not be
  126. output.  Another operation is illustrated in line 53 which is the
  127. not in operation, and should be self explanatory.  You should be
  128. able to see that the message in line 53 will be displayed.  The in
  129. and not in operators are further illustrated in lines 56 and 60
  130. where an explicit range is used for the test range.
  131.  
  132. Be sure to compile and run this program and observe the output.
  133. Here is a chance for you to see if you have a smart compiler.
  134.  
  135.  
  136.  
  137. THE ENUMERATED TYPE
  138. _________________________________________________________________
  139.  
  140. Examine the program named ENUM.ADA for our first ================
  141. look at an enumerated type and how it is used in     ENUM.ADA
  142. a program.  Line 7 is the first definition of an ================
  143. enumerated type, and uses the reserved words
  144. type and is as shown.  The type name is given
  145. between the two reserved words and the values which a variable of
  146. this type is allowed to have assigned to it are given as a list
  147. within parentheses.  The values actually represent numerical values
  148. from 0 up to that value required for the largest value, in this
  149. case 6, since the numbering will be assigned from 0 to 6.  In line
  150. 20, the variable named Day_Of_Week is declared to be of type DAY,
  151. so it can be assigned any of the 7 values listed for the type DAY,
  152. and no others.  We could assign the values 0, 1, 2,.. 6 to
  153. represent the 7 days of the week and use the numerical values
  154. within the program, but by using the enumerated type, we can refer
  155. to Sunday as SUN, Monday as MON, etc., making the program much
  156. clearer and easy to follow.
  157.  
  158. Enumerated types are always locally defined because there are no
  159. predefined enumerated types in the Ada language.  There is one
  160. exception to this, because the BOOLEAN type is an enumerated type
  161. with two possible values, but it has some special properties
  162. available with no other enumerated variables.  These will be
  163. discussed using the next example program.
  164.  
  165. Jumping ahead to the executable code in the current example
  166. program, we illustrate assignment in line 28, where we assign the
  167. value of WED to the variable Day_Of_Week.  Lines 29 and 30
  168. illustrate the FIRST and LAST attributes which we have seen before
  169. for integer type variables.  Just as -32768 is the lowest possible
  170. value that can be assigned to an INTEGER type variable, MON is the
  171. lowest, and hence the first, value that can be assigned to a
  172. variable of type DAY.
  173.  
  174.  
  175.  
  176.                                                          Page 6-3
  177.  
  178.                               Chapter 6 - Additional Scalar Types
  179.  
  180. TWO NEW ATTRIBUTES
  181. _________________________________________________________________
  182.  
  183. Lines 31 and 32 illustrate the attributes PRED, which means the
  184. predecessor, and SUCC, which means the successor.  PRED returns the
  185. value of the predecessor of the present value of the variable used
  186. as an argument.  Since the variable Day_Of_Week was assigned the
  187. value of SUN in line 30, and the day just prior to SUN is SAT, SAT
  188. is assigned to the variable Day_Of_Week in line 31.  It is an error
  189. to attempt to take the PRED of a variable which contains the first
  190. value in the available list, and will result in raising the
  191. exception Range_Error.  Likewise, an attempt to take the SUCC of
  192. any variable that is at its maximum value will result in the
  193. exception Range_Error being raised.  Exceptions will be covered in
  194. detail later in this tutorial.  At this time simply remember that
  195. an exception refers to an exceptional condition or an error.
  196.  
  197.  
  198.  
  199. WHAT IS A SUBTYPE OF AN ENUMERATED TYPE?
  200. _________________________________________________________________
  201.  
  202. In lines 8 and 9, we define two subtypes of the type DAY which will
  203. have all the characteristics of type DAY except for a more
  204. restricted range.  A variable that is declared to be of type
  205. PLAY_DAY can be assigned either of two values, SAT or SUN.  SAT
  206. will have a numerical value of 5, and SUN will have a numerical
  207. value of 6, both of these being inherited from the parent type,
  208. DAY.  Thus in line 32, we use the attribute FIRST to get the first
  209. day of type PLAY_DAY, which will be SAT, then use the attribute
  210. SUCC to get the successor of that value, which will be SUN.  Notice
  211. how the attributes can be combined to obtain the needed
  212. information.  A subtype is assignment compatible with its parent
  213. type.  We will discuss subtypes in greater detail in the next
  214. chapter of this tutorial.
  215.  
  216.  
  217.  
  218. NOW FOR THE POS AND VAL ATTRIBUTES
  219. _________________________________________________________________
  220.  
  221. The POS attribute will return a value of type universal_integer,
  222. the value representing the position of the enumerated value within
  223. the parentheses as shown in lines 33 and 34.  The VAL attribute
  224. will return the enumerated value of the numerical value included
  225. in the parentheses.  Notice that if the type DAY in line 35 were
  226. changed to PLAY_DAY, an error would be returned, since that is an
  227. illegal enumerated value for that type.  The error would be
  228. returned by raising the exception Range_Error.
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.                                                          Page 6-4
  236.  
  237.                               Chapter 6 - Additional Scalar Types
  238.  
  239. WHAT ABOUT ENUMERATED ASSIGNMENTS?
  240. _________________________________________________________________
  241.  
  242. The value of Happy_Day can be assigned to Day_Of_Week at any time
  243. because they are both of the same type, and any value that can be
  244. legally assigned to Happy_Day can also be assigned to Day_Of_Week.
  245. Day_Of_Week cannot always be assigned to Happy_Day however, because
  246. Day_Of_Week is permitted to contain some values which are not legal
  247. to assign to Happy_Day.  This would illustrate that some care must
  248. be exercised when using the enumerated type, but if used properly,
  249. it can help in program debugging by the use of the strong
  250. type-checking defined into the Ada language.
  251.  
  252.  
  253.  
  254. USING ENUMERATED TYPES FOR CONTROL
  255. _________________________________________________________________
  256.  
  257. The loop in lines 37 through 40 covers exactly the range covered
  258. by the subtype WORK_DAY, so we can use it in the range part of the
  259. definition of the loop.  When you run this program, you will see
  260. that the loop will be executed exactly five times.
  261.  
  262. Lines 42 through 51 contain two relational checks on the variable
  263. Today to illustrate that the enumerated type variable can be used
  264. in a BOOLEAN expression.  All of the boolean operators are
  265. available, which includes the following list, and no others;
  266.  
  267.      =     equality
  268.      /=    inequality
  269.      >     greater than
  270.      >=    greater than or equal to
  271.      <     less than
  272.      <=    less than or equal to
  273.  
  274. No mathematical operations are available with enumerated type
  275. variables.  Assignments are available as illustrated in the present
  276. example program.
  277.  
  278.  
  279.  
  280. WHAT IS QUALIFICATION?
  281. _________________________________________________________________
  282.  
  283. In lines 53 and 54, we assign the same value to two different
  284. enumerated type variables.  At least it seems to be the same value.
  285. In actuality, they are two different values with the same name,
  286. namely SUN.  Because Ada does such strong type checking, it is
  287. smart enough to realize that they are actually two different
  288. constants and it will select the one that it needs for each
  289. statement based on the type of the variable to which it will be
  290. assigned.
  291.  
  292.  
  293.  
  294.                                                          Page 6-5
  295.  
  296.                               Chapter 6 - Additional Scalar Types
  297.  
  298. Lines 56 and 57 make the identical assignments by qualifying which
  299. value you are interested in, but in this case, the qualifications
  300. are unnecessary.  There could be a case when you would need to tell
  301. the system which value of SUN you are interested in.  Qualification
  302. uses the type followed by a "tick", or apostrophe, prior to the
  303. enumeration value.
  304.  
  305.  
  306.  
  307. OUTPUTTING ENUMERATED VARIABLES
  308. _________________________________________________________________
  309.  
  310. The statements in lines 59 and 60 output the current value of the
  311. variable Today, and the predecessor of the current value.  Finally,
  312. the same values are output for the variable Big_Sphere, and when
  313. you run the program, you will see that the same value is output for
  314. the first value in each line, but the second values differ for the
  315. two variables.  Note the four extra lines given in program lines
  316. 14 through 18.  These are used to tell the system how to output
  317. enumerated variables, and we will cover the essentials of how this
  318. works very soon.
  319.  
  320. The in and not in operators which we studied in the last program
  321. are available for use with the enumerated type variable.  In fact,
  322. they are available with all discrete types.  Be sure to compile and
  323. run this program and after studying the results, see if you can
  324. modify the program to output additional enumerated values.
  325.  
  326.  
  327.  
  328. THE BOOLEAN VARIABLE
  329. _________________________________________________________________
  330.  
  331. The program named BOOLVARS.ADA is an             ================
  332. illustration of how to use the BOOLEAN variable,   BOOLVARS.ADA
  333. which is actually a special case of an           ================
  334. enumerated variable.  Every enumerated type must
  335. be defined by the user, before use, with the
  336. exception of the predefined BOOLEAN type.  It is simply an
  337. enumerated type with two possible values, TRUE or FALSE.  Since it
  338. is an enumerated type, all of the operations available with the
  339. enumerated type are available with the BOOLEAN type, including all
  340. six of the relational operators, the assignment operator, the
  341. attributes, and no mathematical operators.
  342.  
  343.  
  344.  
  345. THE LOGICAL OPERATORS
  346. _________________________________________________________________
  347.  
  348. The BOOLEAN type has some of its own unique operators that are
  349. available with no other types, the logical operators.  The logical
  350. operators were defined earlier, but are repeated here as a complete
  351. list.
  352.  
  353.                                                          Page 6-6
  354.  
  355.                               Chapter 6 - Additional Scalar Types
  356.  
  357.      and         logical and operation
  358.      or          logical or operation
  359.      xor         exclusive or of two values
  360.      not         inversion of the value
  361.      and then    short circuit and operation
  362.      or else     short circuit or operation
  363.  
  364. It should be pointed out that FALSE is of less numerical value than
  365. TRUE, by definition, and in actuality, the value of FALSE is 0, and
  366. the value of TRUE is 1.
  367. The program illustrates how to output BOOLEAN values in lines 29
  368. and 30, after the package instantiation in lines 7 and 8.  Notice
  369. that the Enumeration_IO library is used for BOOLEAN output
  370. illustrating again that BOOLEAN is a special case of the enumerated
  371. type.
  372.  
  373. Be sure to compile and execute this program to see that it really
  374. does compile as stated.  Someday, you will need the ability to
  375. display the BOOLEAN results as is done in this program.
  376.  
  377.  
  378.  
  379. SOME USELESS ATTRIBUTES OF INTEGER TYPES
  380. _________________________________________________________________
  381.  
  382. It may seem silly to illustrate some useless      ===============
  383. attributes but the program named INCRINT.ADA        INCRINT.ADA
  384. does that very thing.  The POS attribute of an    ===============
  385. integer variable is defined as being the number
  386. itself, and the VAL is also the number.  The
  387. SUCC of an integer variable is the next number, and the PRED is the
  388. predecessor.  These last two attributes could be useful for
  389. incrementing or decrementing a variable in a program, but good
  390. programming practice would forbid such a use of these attributes.
  391. You should use the very clear and easy to understand method of
  392. adding one to the value and assigning the result back to the
  393. variable, as illustrated in line 17 of the program.
  394.  
  395. Even though these are really useless at this point in time, the
  396. fact that this can be done will be very useful when we get to the
  397. study of generics later in this tutorial.
  398.  
  399. Compile and run this program, adding some output to gain some of
  400. your own programming experience.
  401.  
  402.  
  403. FLOATING POINT VARIABLES
  404. _________________________________________________________________
  405.  
  406. Examine the program named FLOATVAR.ADA for       ================
  407. examples of nearly all operations possible with    FLOATVAR.ADA
  408. floating point numbers.                          ================
  409.  
  410.  
  411.  
  412.                                                          Page 6-7
  413.  
  414.                               Chapter 6 - Additional Scalar Types
  415.  
  416. We begin, in lines 7 and 8, by defining two constants, the second
  417. being defined in terms of the first.  Remember that any thing used
  418. in an Ada program must be previously defined, and you will know all
  419. of the rules for defining a value in terms of some other value.
  420. The two constants are of type universal_real, so they can be used
  421. with any of the various real types we will encounter in this
  422. program.  We declare a variable named R in line 10 of type FLOAT,
  423. which is defined by the compiler writer, then two new types of
  424. floating point numbers, and we finally declare six variables of
  425. various types.
  426.  
  427. Two additional floating point types, SHORT_FLOAT and LONG_FLOAT are
  428. defined as optional by the LRM and may be available with your
  429. compiler.  You can find out by checking the documentation supplied
  430. with your compiler or by declaring variables of those types to see
  431. if the compiler will accept the declarations.  If they do exist,
  432. you can determine their limits by using attributes as defined
  433. below.
  434.  
  435.  
  436. HOW TO DECLARE A NEW FLOATING POINT TYPE
  437. _________________________________________________________________
  438.  
  439. The reserved word digits used in line 12 tells the compiler that
  440. you don't care how many bytes of storage it uses to define the
  441. number, but it must store at least 7 significant digits for every
  442. variable of type MY_FLOAT.  Line 13 requests a minimum of 15
  443. significant digits for every variable of type MY_LONG_FLOAT.  Line
  444. 10, on the other hand, only requires that it be a floating point
  445. number, and the compiler writer has the option of using as many
  446. significant digits as he desires to implement variables of this
  447. type.  If you wrote a program that ran well with one compiler, it
  448. may not run properly with a different compiler, either because the
  449. new one did not use enough significant digits, or because the new
  450. one used far more causing your program to run out of storage space
  451. or run too slowly.  The forms in lines 12 and 13 are therefore
  452. preferred for portability purposes.  More will be said about
  453. declaring floating point types in the next chapter of this
  454. tutorial.
  455.  
  456.  
  457. FLOATING POINT LITERALS
  458. _________________________________________________________________
  459.  
  460. The distinguishing characteristic that defines a floating point
  461. number is the use of a decimal point.  Ada requires at least one
  462. digit before and after the decimal point, although either or both
  463. may be zeros.  Single embedded underlines are allowed to improve
  464. readability, but cannot be adjacent to the decimal point.  The
  465. underlines are ignored by the compiler, and have no significance.
  466. Any radix from 2 through 16 may be used by first giving the radix,
  467. then enclosing the number in pound (#) characters.  The base 10 is
  468. the default and need not be specified.  Exponential notation can
  469. be used, the exponent being to the same base as that indicated by
  470.  
  471.                                                          Page 6-8
  472.  
  473.                               Chapter 6 - Additional Scalar Types
  474.  
  475. the radix.  A binary floating point literal is illustrated in line
  476. 31 of the program and you can see that the radix is similar to that
  477. used for integer class literals.
  478.  
  479.  
  480. FLOATING POINT MATHEMATICAL OPERATORS
  481. _________________________________________________________________
  482.  
  483. Lines 29 through 35 illustrate the mathematical operators available
  484. with floating point variables and should be self explanatory with
  485. the exception of the exponential operator.  This can use only an
  486. integer type of exponent but it can be either positive or negative.
  487. Of course, zero is also permissible.
  488.  
  489. All six logical comparisons are available with floating point
  490. variables as illustrated in lines 38 through 43.  The next two
  491. lines, 45 and 46, illustrate some multiple mathematical operations.
  492.  
  493. As with all variables, the types must agree within all mathematical
  494. and logical operations and the result must be assigned to the right
  495. type of variable, or a type error will be generated at compile
  496. time.  In line 46, the variable Area must be transformed in type
  497. prior to being assigned to Cover since they are of different types.
  498. The entire statement will be evaluated as of type MY_LONG_FLOAT,
  499. since that will be the final result.  The constant 27.3, and the
  500. constant PI, will be transformed automatically from universal_real
  501. to MY_LONG_FLOAT prior to the multiplications.
  502.  
  503.  
  504.  
  505. NOW TO OUTPUT SOME FLOATING POINT VALUES
  506. _________________________________________________________________
  507.  
  508. In lines 24 and 25 we instantiate a copy of the Float_IO package
  509. for use with the type MY_FLOAT and use it in lines 49 through 55.
  510. The variable Area will be output in a default exponential notation
  511. in line 49, but with 5 digits prior to the decimal point in line
  512. 50.  Line 54 adds an additional 5, which will cause 5 digits to be
  513. output following the decimal point, and the fourth field, in this
  514. case a zero, causes the output to be written with a zero exponent,
  515. or no exponential notation.
  516.  
  517.  
  518.  
  519. FLOATING POINT ATTRIBUTES
  520. _________________________________________________________________
  521.  
  522. Floating point variables and types are no different from the scalar
  523. types concerning attributes available for your use, except that
  524. there are different attributes available.  Lines 58 through 72
  525. illustrate the use of some of the available attributes.  The
  526. attribute named DIGITS, gives the number of significant digits
  527. available with the specific type, and the return is a
  528. universal_integer type.
  529.  
  530.                                                          Page 6-9
  531.  
  532.                               Chapter 6 - Additional Scalar Types
  533.  
  534.  
  535. The attributes named SMALL and LARGE give the smallest and largest
  536. numbers available with the corresponding type, and the attributes
  537. named FIRST and LAST combined with the BASE attribute as shown in
  538. lines 68 and 71, define the extreme values as used by the
  539. underlying base type of the actual user's type.  All four of these
  540. attributes return a value of the type universal_real, and are
  541. displayed on the monitor for your information.  Note that there are
  542. other attributes available with the floating point type, but only
  543. these will be elaborated upon at this time.
  544.  
  545. See appendix A of the LRM for additional information on attributes.
  546. This appendix lists all of the attributes available with an Ada
  547. system.
  548.  
  549. Compile and run this program and observe the output.  The actual
  550. output is the clearest description of the Put procedure when used
  551. with floating point numbers.  Study the result of the attribute
  552. outputs before continuing on to the next example program.
  553.  
  554.  
  555.  
  556. FIXED POINT VARIABLES
  557. _________________________________________________________________
  558.  
  559. Fixed point variables are a relatively new        ===============
  560. concept and may be a bit confusing, but the file     FIXED.ADA
  561. named FIXED.ADA will illustrate the use of a few  ===============
  562. fixed point variables.  Line 9 defines a fixed
  563. point type as having a range of -40.0 to 120.0,
  564. and a delta of 0.1 which means that a variable of this number can
  565. only have a value that is accurate to one digit after the decimal
  566. point.  There are therefore a fixed number of digits before and
  567. after the decimal point, hence the name of this type of variable.
  568.  
  569.  
  570. A fixed point number will always be exact since it is defined that
  571. way.  There can never be a gradual accumulation of error with a
  572. fixed point variable.  In order to completely understand the fixed
  573. point type, one would require a complete understanding of numerical
  574. analysis, which is beyond the scope of this tutorial.  The program
  575. before you will illustrate how to use this type, but no attempt
  576. will be made to explain why it should be used.
  577.  
  578. There are no predefined fixed point types, so it is up to the
  579. programmer to define every fixed point type needed, as illustrated
  580. in lines 9 and 10.  The reserved word delta denotes a fixed point
  581. type and a range is required for every fixed point type.  Lines 12
  582. and 13 are used to declare a few variables for use in the program,
  583. then lines 17 through 20 instantiate the package Fixed_IO for use
  584. with our two fixed point types.
  585.  
  586.  
  587.  
  588.  
  589.                                                         Page 6-10
  590.  
  591.                               Chapter 6 - Additional Scalar Types
  592.  
  593. HOW DO WE USE FIXED POINT TYPES?
  594. _________________________________________________________________
  595.  
  596. Output of a fixed point type uses the same format as that defined
  597. for floating point data as shown in line 27.  When we come to
  598. arithmetical operations, we find some funny rules which we will
  599. simply state, and make no attempt to justify.  Variables of the
  600. same fixed point types can be added and subtracted freely, provided
  601. the results are within the defined range, just like floating point
  602. type variables.  Multiplication by a constant of type
  603. universal_integer is permitted, resulting in the same fixed point
  604. type we started with.  Multiplication of two fixed point variables
  605. results in an anonymous type which must be explicitly converted to
  606. some predefined type, as illustrated in lines 33 and 34.  The only
  607. operator available with the fixed types is the abs operator.
  608.  
  609. Many attributes are available with the fixed point type, some of
  610. which are illustrated in lines 46 through 61.  The attributes named
  611. DELTA, SMALL, and LARGE, each return a value which is of type
  612. universal_real, and must be converted to the users fixed type, by
  613. a type conversion, before the result can be used in the program.
  614. Line 48 illustrates the conversion within the Put procedure call.
  615. Lines 51 and 54 illustrates the explicit conversion to a FLOAT
  616. type, but since the results are to be used as FLOAT variables, and
  617. the universal_real type can be used directly as a FLOAT type, the
  618. conversion is not actually required.
  619.  
  620. Be sure to compile and run this program and observe the output to
  621. see if it conforms to what you think it should do based on the
  622. previous discussion.  Note that your compiler may not generate
  623. identical output as that listed in the result of execution due to
  624. different compiler defaults.
  625.  
  626.  
  627.  
  628. MIXING VARIOUS TYPES
  629. _________________________________________________________________
  630.  
  631. Examine the program named MIXTYPES.ADA for       ================
  632. examples of using various types together.  It is   MIXTYPES.ADA
  633. meant to be an illustration of how to combine    ================
  634. some of the various types available in Ada.
  635. Many type transformations are illustrated in
  636. this program and should be easy for you to understand.  Note
  637. especially, that the final result of lines 27, 28, and 29 will not
  638. necessarily be the same due to the rounding that takes place at
  639. different points in the calculations.
  640.  
  641. Note that in Ada, conversion from real to integer always rounds
  642. rather than truncates.  A value midway between the two integer
  643. values can go either way since it is not defined by the LRM but is
  644. left up to the implementor.
  645.  
  646.  
  647.  
  648.                                                         Page 6-11
  649.  
  650.                               Chapter 6 - Additional Scalar Types
  651.  
  652. Compile and execute this program to assure yourself that it will
  653. compile correctly.
  654.  
  655.  
  656. PROGRAMMING EXERCISES
  657. _________________________________________________________________
  658.  
  659. 1.   Write a program to determine if LONG_INTEGER and SHORT_INTEGER
  660.      types are available with your compiler.  If they are
  661.      available, use attributes to determine their characteristics.
  662.  
  663. 2.   Do the same thing as exercise 1 for the LONG_FLOAT and
  664.      SHORT_FLOAT types.
  665.  
  666. 3.   Try to take the PRED of the first element of an enumerated
  667.      variable to see what kind of a run-time error you get.  Your
  668.      compiler may be smart enough to warn you if you try to take
  669.      it directly (i.e. - by using the first value in the
  670.      parentheses), so you may need to assign a variable to the
  671.      first value and take the PRED of the variable.
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.                                                         Page 6-12