Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 36 ยท Types and Control Flow

Integer and Floating-Point Types in Ada

Define your own numeric types in Ada with range constraints, modular types, subtypes, floating-point digits and fixed-point deltas, and see how overflow is caught.

In most languages you pick int or long and hope it is large enough. In Ada you describe what you need: a type whose values lie between 0 and 100, or an angle with six decimal digits. The compiler picks a machine representation, inserts checks, and rejects mixing of unrelated types. This is the heart of what makes Ada code safe.

Ada listings are read-only here; C stand-ins show the matching behavior.

Predefined and user-defined integers

Integer, Natural (0 and up), Positive (1 and up), Long_Integer and a few others are predefined. Better still, define a type with the range you mean:

ADA
type Age        is range 0 .. 150;
type Temperature is range -90 .. 60;
type Day_Of_Year is range 1 .. 366;

Each is a distinct type. Adding an Age to a Temperature is a compile error. The compiler chooses a suitable size (here 8 or 16 bits). An assignment that leaves the range raises Constraint_Error at run time:

ADA
A : Age := 150;
begin
   A := A + 1;   --  Constraint_Error: 151 is outside 0 .. 150

Subtypes

A subtype narrows a type without creating a new, incompatible one:

ADA
subtype Percent is Integer range 0 .. 100;
subtype Hour    is Integer range 0 .. 23;

P : Percent := 50;
I : Integer := P;      --  fine: same base type

Natural and Positive are subtypes of Integer. Use subtypes when values should still mix with the parent type, and new types when they should not (for example meters versus seconds).

Modular types

A modular type wraps around, like unsigned integers in C, and the modulus can be any number:

ADA
type Byte    is mod 2**8;     --  0 .. 255
type Hour24  is mod 24;       --  0 .. 23, wraps
type Counter is mod 2**32;

Byte'(250) + 10 gives 4. Modular types also provide bitwise and, or, xor and not, so they are the Ada tool for bit manipulation and hardware registers.

Floating-point types

You request precision in decimal digits, not "float or double":

ADA
type Weight is digits 6;                      --  at least 6 significant digits
type Voltage is digits 8 range -12.0 .. 12.0;

The compiler maps digits 6 to a 32-bit float and digits 15 to a 64-bit one on typical hardware. Float and Long_Float are predefined. As in every language, never compare floating-point values for equality after arithmetic; compare with a tolerance.

Fixed-point types

Ada has a feature few languages offer: real numbers with a fixed, exact step, ideal for money and sensor data.

ADA
type Cents  is delta 0.01 range 0.00 .. 1_000_000.00;
type Angle  is delta 2.0**(-12) range -1.0 .. 1.0;

The delta is the absolute step. Arithmetic is performed on scaled integers, so there are no binary rounding surprises for decimal steps (delta 0.01 with digits for decimal fixed point). Embedded software uses these to avoid floating-point hardware.

The type hierarchy

flowchart TD
    S["Scalar types"] --> D["Discrete"]
    S --> R["Real"]
    D --> I["Integer: signed and modular"]
    D --> E["Enumeration, Character, Boolean"]
    R --> F["Floating point (digits)"]
    R --> X["Fixed point (delta)"]

Discrete types can index arrays and drive loops; real types cannot.

Useful attributes

Attributes are queries about a type, written with a tick:

ADA
Age'First         --  0
Age'Last          --  150
Age'Size          --  number of bits
Integer'Last      --  largest Integer
Day_Of_Year'Pos (5)  --  position number
Float'Digits      --  precision

Stand-in: range checks, wrap-around and fixed point in C

C has no range types, so this labelled stand-in does by hand what Ada does for you: it raises an error for out-of-range arithmetic on Age, wraps a mod 24 value, and stores money as integer cents like delta 0.01.

C Stand-in: range, modular and fixed-point arithmetic

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Fixed-point values never accumulate binary rounding error, because every number is an exact multiple of the delta. A second stand-in shows the contrast with binary floating point:

C Stand-in: why decimal steps matter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens at run time when you assign 151 to a variable of type Age is range 0 .. 150?

    Range constraints are checked; violating one raises Constraint_Error.
  2. 2. When should you declare a subtype instead of a new type?

    Subtypes share the parent's base type, so values mix freely; new types are incompatible by design.
  3. 3. What is Byte'(250) + 10 for type Byte is mod 2**8?

    Modular types wrap around, so 260 mod 256 = 4.
  4. 4. What does type Cents is delta 0.01 range 0.0 .. 100.0; declare?

    delta introduces a fixed-point type whose values are multiples of the step.

Summary

  • Define integer types by range, and the compiler picks the representation and checks every assignment.
  • Subtypes narrow a type while remaining compatible; new types are incompatible on purpose.
  • Modular types wrap and support bitwise operators; floats use digits; fixed point uses delta.
  • Attributes such as 'First, 'Last and 'Size describe types.

Next: Enumerations, Characters and Booleans