Tuesday 3/12/01 Week 9

Chapter 8

Student Notes

 

Objectives

Numeric data types

the USE clause

Random numbers

Boolean types

Character types

CASE statements

 

1. Numeric data types

·        the predefined numeric types Integer, Natural, Positive, Float represent numeric information

·        There are differences between these types that we have discussed before

·        these types are all               types.

·         

·         

·        Why do we use these different types when it seems as though we could you floats for all the four types?

·         

·         

·         

·         

·         

·         

·         

·        e.g. page 303

 

2. Numeric Literals

·        A constant value that appears in an expression is a

·        A float literal must have at least

·         

·        a literal may also have a scale factor. e.g. 2.998E+5 the scale factor is 105

·        it is also possible to use underscores not commas to separate groups of digits (30_000)

·        we can also use literals of different bases

·        2#11101# is 29 in binary, 8#34# is 29 in octal, and 16#1D# is 29 in hexadecimal

 

3. Numeric Operators

·        there are two different kinds of operators  

·        a monadic operator takes a single operand

·        a dyadic operator takes two operands

 

 

·        tables page 305

 

4. Multiple-Operator expressions revisited

·        expressions in parentheses are evaluated first

·        Operator precedence

            **, ABS       -- first

     *, /, REM     -- next

     +, - (monadic) –- next

     +, - (dyadic)  -- last

·        left associative (operators of equal precedence are evaluated left to right)

·        expression trees may help you construct formulas in your code (page 309, 310)

 

5.  type conversions

·        Ada does not allow mixing types in expressions, however

·         

·         

·        F: Float;

N: NonNegFloat;

I: Integer;

P: Positive;

T: Natural;

 

F:= Float(I);    --always possible

N:= Float(p);    -- always possible

I := Integer(F); -- always possible, result rounded

I := Integer(N); -- always possible, result rounded

 

n := NonNegFloat(I); -- Constraint_Error if I is neg

T := Natural(F);      -- Constraint_Error if F is neg

 

I := Integer(5.49);   -- 5

I := Integer(5.50);   -- 6

I := Integer(5.51);   -- 6

 

I := Integer(-5.3);   -- -5

I := Integer(-5.6);   -- -6

I := Integer(-5.5);   -- -6

 

 

 

 

 

 

6. Ada.Numerics and the USE clause

·        Ada.Numerics also provides arctangent, cosine, exponent, logarithmic, sine, and square root functions (page 316)

·        the USE clause may be used to write terse code. (page 317)

·         

·         

·         

·        see program 8.4 for an example with Ada.Numerics and the USE clause

 

7. random numbers

·        You can get a random number within a specific range of values

·        First create a new random number generator package (just like the Enumeration_IO package)

·         SUBTYPE Fifty IS Integer RANGE 1..50;

PACKAGE Randome50 IS

   NEW Ada.Numerics.Discrete_Random(Result_Subtype =>Fifty);

 

to get a random number from this package, declare a variable of the subtype Fifty, we’ll call ours G

 

Number := Random50.Random(Gen => G);

 

G is used as a “Seed” for the random number generator, every time you use the same seed, you will get the same random number. To reset the seed

 

Random50.Reset(Gen => G);  -- where G has a new value

 

·        page 327 for an example

 

8. Boolean Types

  • TYPE Boolean IS (False, True); the type definition
  • GrossPay > TaxBracket a Boolean expression
  • There is the Boolean type which is the actual value True or False
  • There are also Boolean expressions which evaluate to a Boolean type

 

9. Boolean Operators

  • AND  
  • OR
  • XOR (exclusive OR)
  • NOT
  • page 330 – table of Boolean operators

 

10. Operator Precedence (Again)

  • **, NOT, ABS       --evaluated first
  • *, /, REM          --multiplicative
  • +, - (monadic)
  • +, -, & (Dyadic)   -- (& is for append, Chapter 10)
  • <, <=, =, /=, >=, > -- relational operators
  • AND, OR, XOR       -- dyadic logical operators (last)
  • ex. #7 page 331

 

11. Short Circuit Operators

  • so far we are evaluating both sides of the Boolean operator at the same time, but we can force one side to evaluate before another
  • Here both sides are evaluated: NOT Flag OR ((Y+Z) /= (X-Z))
  • But here the left hand side is evaluated first:
  • NOT Flag OR ELSE ((Y+Z) /= (X-Z))

 

12. Boolean Assignment Statements

  •  
  •  
  •  
  • Same := X=Y; is the same as:

IF X = Y THEN

  Same := True;

ELSE

  Same := False;

END IF;

 

  • Boolean variables are also very useful as program flags
  • page 339

 

13. Boolean IO

  • Ada does not provide a Boolean IO, however we can create a package using  Enumeration_IO
  •  
  •  
  •  
  • page 335

 

14. The Character Type

  • a character is any printable character
  • to specify a Character value, enclose the printable character in single quotes
  • Star : CONSTANT Character := ‘*’;
  • page 341 for the character type definition
  • characters are evaluated by their position in this definition
  • ‘0’ < ’9’ < ‘A’ < ‘Z’ < ‘a’ < ‘z’
  • you may use relational operators with characters, but they will be evaluated in this order
  • It is possible to print the non-printable characters
  • Ada.Text_IO.Put(Item => Character’Val(10)); (the line feed)
  • Ada.Text_IO.Put(Item => Character.LF); (using the given name)

 

15. The CASE statement

  • they reformat and simplify a long IF statement
  • page 347