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:
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?
![]() |
![]() |
![]() |
---|
David A. Wheeler (dwheeler@ida.org)
The master copy of this file is at "http://www.adahome.com/Tutorials/Lovelace/s6s1.htm".