A real is a number that can (but doesn't have to) contain a fractional part. Examples include 1.5, -0.33335, and
1.987E3. (The last example is in so-called exponential (or scientific) notation, and is read as "1.987 times
10-to-the-3" and is usually written as 1.987 × 10³.)
Pascal variables of type real (also called floating-point numbers) have a vast range--all the way from 1.0E-38
(a number very close to zero: 0.000 000 000 000 000 000 000 000 000 000 000 000 01) to 1.0E+38 (an enormous number:
100 000 000 000 000 000 000 000 000 000 000 000 000).
Real numbers can be positive or negative and require 6 bytes of storage each.
Accuracy Limitations
Unlike integers and long integers, Pascal's internal representation of reals is not
exact. For one thing, no number is known to more than eleven digits of accuracy. For example, 123 456 654 321 would
be represented as a real as 1.234566543E+11 (123 456 654 300; the last two digits of precision are lost).
Furthermore, because of the internal scheme used to represent reals, most decimal fractions, even those with fewer
than eleven digits, cannot be represented exactly. For example, $1.49 can only be approximated by a real. That
approximation is very good (somewhere between $1.489999999 and $1.490000001), but lacks the absolute precision
of integers.
Delphi Pascal offers five additional floating-point types--single, double, extended, comp, and currency--that offer
more range and accuracy than type real, with an accompanying trade-off in storage requirement and calculation speed.
Formatting Reals
To output a real in a format other than the default scientific notation, use "write parameters"; two
integers of formatting information included after a real expression in a Write or Writeln call. For example:
Writeln(R:12:2);
outputs real variable R in a more-standard decimal form (right justified in a field 12 columns wide, with two digits
after the decimal point).
program
RealAccuracyDemo;
var R:
real;
begin
R := 12345654321.0; {
reals have 11-digit accuracy }
Writeln(R:15:0);
R := 1234567654321.0; { a 13-digit value }
Writeln(R:15:0);
R := 123456787654321.0; { a 15-digit value
}
Writeln(R:15:0);
end. { RealAccuracyDemo }