Ada Cheat Sheets

From ezUnix
Jump to: navigation, search
                                    pdf_icon.png Download this article as a single PDF document 

A list of some quick Ada specific options

Integers

Integer attributes:

   First           -- the first (lowest) value of the type
   Last            -- the last (highest) value of the type
   Image(X)        -- convert the integer X to a string
   Value(X)        -- convert the string X to an integer
   Min(X,Y)        -- the smaller of the integers X and Y
   Max(X,Y)        -- the larger of the integers X and Y

Arithmetic operations

    +    Addition           -    Subtraction
    *    Multiplication     /    Division
    rem  Remainder          mod  Modulus
   **   Exponentiation     abs  Absolute value


Highest priority (evaluated first):  **    abs   not
                                     *     /     mod   rem
                                     +     -     (unary versions)
                                     +     -     &
                                     =     /=    <     >     <=    >=
Lowest priority (evaluated last):    and   or    xor


Difference between rem and mod is the way they deal with negative numbers: rem gives a negative result if the dividend (the left hand operand) is negative, whereas mod gives a negative result if the divisor (the right hand operand) is negative.

7/5     =  1     7 rem 5     =  2      7 mod 5     =  2
(-7)/5  = -1      (-7) rem 5  = -2     (-7) mod 5  =  3
7/-5    = -1     7 rem -5    =  2      7 mod -5    = -3
(-7)/-5 =  1      (-7) rem -5 = -2     (-7) mod -5 = -2


Real types

type My_Float is digits 10;                                -- a floating point type
type My_Fixed is delta 0.01 range 0.0 .. 10.0;     -- a fixed point type
type Decimal  is delta 0.01 digits 12;                 -- a decimal type (delta
                                                                     -- must be a power of 10)

My_Float is a floating point type which is accurate to at least ten significant figures, and My_Fixed is a fixed point type which is accurate to within 0.01 (i.e. to at least two decimal places) across the specified range. Decimal is a decimal type with 12 digits which is accurate to two decimal places (i.e. capable of representing decimal values up to 9999999999.99).


Numeric literals

It’s possible to write numbers in binary or hexadecimal or any other base between 2 and 16. Here are three different ways of writing the decimal value 31:

2#11111#   -- the binary (base 2) value 11111
16#1F#       -- the hexadecimal (base 16) value 1F
6#51#         -- the base 6 value 51

The letters A to F (or a to f) are used for the digits 10 to 15 when using bases above 10. If you mix an exponent (e) part with a based number, the exponent is raised to the power of the base; thus 16#1F#e1 means hexadecimal 1F (= 31) × 161, or 496 in decimal.


Enumerations

Pos(X)    -- an integer representing the position of X in the
                list of possible values starting at 0
Val(X)    -- the X'th value in the list of possible values
Succ(X)   -- the next (successor) value after X
Pred(X)   -- the previous (predecessor) value to X


Cheers,

YazzY