Difference between revisions of "Real"

From Lazarus wiki
Jump to navigationJump to search
m
(→‎alternatives: del dead svn link)
 
(8 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
{{Real}}
 
{{Real}}
  
'''real''' is a [[Standard type|standard type]] of the [[Pascal]] programming language. It is used to represent rational numbers, which may contain a decimal point and an exponent, as opposed to the [[Integer]] data type which is used to represent whole numbers.
+
<syntaxhighlight lang="pascal" inline>real</syntaxhighlight> is a [[Standard type|standard type]] of the [[Pascal]] programming language.
  
The internal representation of the type real (i.e. number of bytes and byte ordering) and the resulting range and precision are platform dependent.
+
== usage ==
 +
Floating-point literals can be assigned to <syntaxhighlight lang="pascal" inline>real</syntaxhighlight> variables.
 +
<syntaxhighlight lang="pascal" line>
 +
program realDemo(input, output, stderr);
  
Citation from the FreePascal Programmer's Manual (Chapter 8.2.5 Floating point types):
+
var
 +
r: real;
  
Contrary to Turbo Pascal, where the real type had a special internal format, under Free Pascal the
+
begin
real type simply maps to one of the other real types. It maps to the double type on processors
+
r := 0.; // r becomes zero
which support floating point operations, while it maps to the single type on processors which do
+
writeLn(r, ' ', r:8:4);
not support floating point operations in hardware.
+
 +
r := 1e2; // r becomes 1*(10^2) [a hundred]
 +
writeLn(r, ' ', r:8:4);
 +
 +
r := r / r; // r becomes one
 +
writeLn(r, ' ', r:8:4);
 +
end.
 +
</syntaxhighlight>
 +
<syntaxhighlight lang="pascal" inline>real</syntaxhighlight>, like all floating-point types, supports [[Slash#division|<syntaxhighlight lang="pascal" inline>/</syntaxhighlight> division operator]], while integer types support <syntaxhighlight lang="pascal" inline>div</syntaxhighlight>-operator.
  
So, most common is the mapping to double (8 bytes, 64 bit), with 1 sign bit, 11 exponent bits and 52 mantissa bits. The range of numbers is given by the largest positive and smallest negative numbers: ±1.7976931348623157×10<sup>308</sup>. The smallest positive non-zero and largest negative non-zero numbers are: ±2.2250738585072020×10<sup>−308</sup>.
+
== internal representation ==
The 52 mantissa bits translate into a decimal precision of 15 digits.
+
The internal representation of the type <syntaxhighlight lang="pascal" inline>real</syntaxhighlight> (i.e. number of bytes and byte ordering) and the resulting range and precision are platform dependent.
  
A more detailed description of floating point numbers is given on the wikipedia pages for [http://en.wikipedia.org/wiki/Floating_point floating point numbers] and [http://en.wikipedia.org/wiki/IEEE_754 IEEE 754] .
+
Quote from the “FreePascal Programmer's Manual” (Chapter 8.2.5 [https://www.freepascal.org/docs-html/prog/progsu158.html Floating point types]):
 +
<blockquote>
 +
Contrary to Turbo Pascal, where the <syntaxhighlight lang="pascal" inline>real</syntaxhighlight> type had a special internal format, under Free Pascal the <syntaxhighlight lang="pascal" inline>real</syntaxhighlight> type simply maps to one of the other real types.
 +
It maps to the [[Double|<syntaxhighlight lang="pascal" inline>double</syntaxhighlight> type]] on processors which support floating point operations, while it maps to the [[Single|<syntaxhighlight lang="pascal" inline>single</syntaxhighlight> type]] on processors which do not support floating point operations in hardware.
 +
</blockquote>
 +
 
 +
== alternatives ==
 +
Instead of specifying <syntaxhighlight lang="pascal" inline>real</syntaxhighlight> in your code, write {{Doc|package=RTL|unit=system|identifier=valreal|text=<syntaxhighlight lang="pascal" inline>ValReal</syntaxhighlight>}}, a type alias defined by the [[System unit]].
 +
It automatically maps to the largest available floating point type available: <syntaxhighlight lang="pascal" inline>single</syntaxhighlight>, <syntaxhighlight lang="pascal" inline>double</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>extended</syntaxhighlight>.
  
 
{{Data types}}
 
{{Data types}}

Latest revision as of 17:36, 6 March 2022

Deutsch (de) English (en) français (fr) русский (ru)

real is a standard type of the Pascal programming language.

usage

Floating-point literals can be assigned to real variables.

 1program realDemo(input, output, stderr);
 2
 3var
 4	r: real;
 5
 6begin
 7	r := 0.; // r becomes zero
 8	writeLn(r, ' ', r:8:4);
 9	
10	r := 1e2; // r becomes 1*(10^2) [a hundred]
11	writeLn(r, ' ', r:8:4);
12	
13	r := r / r; // r becomes one
14	writeLn(r, ' ', r:8:4);
15end.

real, like all floating-point types, supports / division operator, while integer types support div-operator.

internal representation

The internal representation of the type real (i.e. number of bytes and byte ordering) and the resulting range and precision are platform dependent.

Quote from the “FreePascal Programmer's Manual” (Chapter 8.2.5 Floating point types):

Contrary to Turbo Pascal, where the real type had a special internal format, under Free Pascal the real type simply maps to one of the other real types. It maps to the double type on processors which support floating point operations, while it maps to the single type on processors which do not support floating point operations in hardware.

alternatives

Instead of specifying real in your code, write ValReal, a type alias defined by the System unit. It automatically maps to the largest available floating point type available: single, double or extended.


navigation bar: data types
simple data types

boolean byte cardinal char currency double dword extended int8 int16 int32 int64 integer longint real shortint single smallint pointer qword word

complex data types

array class object record set string shortstring