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
·
·
·
·
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
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
9. Boolean Operators
10. Operator Precedence (Again)
11. Short Circuit Operators
12. Boolean Assignment Statements
IF
X = Y THEN
Same := True;
ELSE
Same := False;
END
IF;
13. Boolean IO
14. The Character Type
15. The CASE statement