Section 6.1 - Type Float

For the next few sections we'll learn a little more about Ada types. We've already seen Ada's built-in type Integer.

Like Integer, Ada also has a predefined type called Float. Float can store fractional values and `large' values. Float is used when you don't care about the minimum or maximum range of numbers, nor about the minimum accuracy of the value - the Ada compiler will choose whatever is most `natural' for the machine. Type `Float' is not appropriate if you do care about the range or accuracy; Ada has ways to specify these.

Float has all the arithmetic (+, -, *, /, **, etc.) and comparison (=, /=, >, >=, <, <=) operations you'd expect. The ``**'' operator means ``exponentiate''.

Ada insists that types be correct in operations, and there aren't any predefined operations for mixing Integer and Float using +, -, *, or /. Thus, if you're using an Integer and Float together, put a function called `Float()' around the Integer variables to cause them to be converted into floating-point values. This makes it clear when such conversions are taking place, which is sometimes important in understanding what a program is doing. Also, whenever you set a Float to a constant, the constant must be written with a period in it, or the compiler will complain.

Here are some examples:

 with Ada.Float_Text_IO;
 use Ada.Float_Text_IO;
 procedure Think is
   A, B : Float := 0.0; -- A and B initially zero; note the period.
   I, J : Integer := 1;
   A := B + 7.0;
   I := J * 3;
   B := Float(I) + A;
 end Think;

It's important that you understand the general limitations of floating point numbers on digital computers. Floating point numbers are usually stored as a binary approximation using a limited number of bits. The upshot is that results are usually only approximately the value you'd expect. Even numbers that are represented exactly in decimal may only be approximate when converted to an internal floating point number. Thus, be wary of using "=" to compare two floating point numbers. This isn't specific to Ada - Fortran, C, Pascal, and so on all do the same thing. For more information on this subject, see the survey by David Goldberg [1991] on floating-point arithmetic.


In procedure Think defined above, what will be printed out as the final value of B?

  1. 10.0
  2. 0.0
  3. 7.0

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 6 outline

David A. Wheeler (

The master copy of this file is at "".