Previous topic |
Next topic |
Ada Home Page |
Index
FLOAT type
A FLOAT is a positive or negative number with a decimal part
- real (floating point) number
- may be scientific notation
FLOAT literals: optional sign, digits, period, digits
123.456
1.23456E2
0.0001234
1.234e-4
Why use floats?
- numbers with decimal parts
- larger range of numbers than INTEGER handles
BUT
- inexact
- slower than integers; more storage
Range of FLOATs
Attributes
- FLOAT'DIGITS no of significant figures
- FLOAT'FIRST minimum on given system
- FLOAT'LAST maximum on given system
- FLOAT'SMALL smallest on given system
Example:
PUT ("The number of digits in a float value is: ");
PUT (FLOAT'DIGITS); NEW_LINE;
PUT ("The lowest float value is: ");
PUT (FLOAT'FIRST); NEW_LINE;
PUT ("The highest float value is: ");
PUT (FLOAT'LAST); NEW_LINE;
PUT ("The tiniest float value is: ");
PUT (FLOAT'SMALL); NEW_LINE;
The number of digits in a float value is: 15
The lowest float value is: -1.79769313486232E+308
The highest float value is: 1.79769313486232E+308
The tiniest float value is: 1.94469227433161E-62
Errors with FLOATs
Since floating point numbers are stored with only a fixed number of significant
digits of accuracy, they are subject to rounding errors. Four types of
errors arise from this:
- representation error (rounding error)
- cancellation error: adding a large and small number, the larger number
may cancel the smaller
- arithmetic underflow: two very small numbers are multiplied, giving a
result that is too small to represent
- arithmetic overflow: two very large numbers are multiplied, giving a
result that is too large to represent
Operations on FLOATs
I/O: special library provides GET, PUT
package float_io is new TEXT_IO.FLOAT_IO( FLOAT );
use float_io;
arithmetic
- unary minus (negation) - float_val
- absolute value abs float_val
- + - * /
- ** (exponentiation) requires integer exponent
relational:
- = /= < > <= >=
- do not test for exact "equality", because of rounding errors
(an example program
demonstrates this).
Previous topic |
Next topic |
Ada Home Page |
Index
c-lokan@adfa.oz.au / 12 Feb 96