Difference between revisions of "Real"

From Lazarus wiki
Jump to navigationJump to search
(Real is not a rational number (see: https://en.wikipedia.org/wiki/Rational_number))
(fix link, replace legacy syntaxhighlight syntax)
Line 1: Line 1:
 
{{Real}}
 
{{Real}}
  
<syntaxhighlight lang="pascal" enclose="none">real</syntaxhighlight> is a [[Standard type|standard type]] of the [[Pascal]] programming language.
+
<syntaxhighlight lang="pascal" inline>real</syntaxhighlight> is a [[Standard type|standard type]] of the [[Pascal]] programming language.
  
 
== usage ==
 
== usage ==
Floating-point literals can be assigned to <syntaxhighlight lang="pascal" enclose="none">real</syntaxhighlight> variables.
+
Floating-point literals can be assigned to <syntaxhighlight lang="pascal" inline>real</syntaxhighlight> variables.
 
<syntaxhighlight lang="pascal" line>
 
<syntaxhighlight lang="pascal" line>
 
program realDemo(input, output, stderr);
 
program realDemo(input, output, stderr);
Line 22: Line 22:
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
Note, that the result of the [[Slash#division|<syntaxhighlight lang="pascal" enclose="none">/</syntaxhighlight>-division]] can be assigned.
+
Note, that the result of the [[Slash#division|<syntaxhighlight lang="pascal" inline>/</syntaxhighlight>-division]] can be assigned.
  
 
== internal representation ==
 
== internal representation ==
The internal representation of the type <syntaxhighlight lang="pascal" enclose="none">real</syntaxhighlight> (i.e. number of bytes and byte ordering) and the resulting range and precision are platform dependent.
+
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.
  
 
Quote from the “FreePascal Programmer's Manual” (Chapter 8.2.5 [https://www.freepascal.org/docs-html/prog/progsu159.html Floating point types]):
 
Quote from the “FreePascal Programmer's Manual” (Chapter 8.2.5 [https://www.freepascal.org/docs-html/prog/progsu159.html Floating point types]):
 
<blockquote>
 
<blockquote>
Contrary to Turbo Pascal, where the <syntaxhighlight lang="pascal" enclose="none">real</syntaxhighlight> type had a special internal format, under Free Pascal the <syntaxhighlight lang="pascal" enclose="none">real</syntaxhighlight> type simply maps to one of the other real types.
+
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" enclose="none">double</syntaxhighlight> type]] on processors which support floating point operations, while it maps to the [[Single|<syntaxhighlight lang="pascal" enclose="none">single</syntaxhighlight> type]] on processors which do not support floating point operations in hardware.
+
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>
 
</blockquote>
  
 
== alternatives ==
 
== alternatives ==
Instead of specifying <syntaxhighlight lang="pascal" enclose="none">real</syntaxhighlight> in your code, write {{Doc|package=RTL|unit=system|identfier=valreal|text=<syntaxhighlight lang="pascal" enclose="none">valReal</syntaxhighlight>}}, a type alias [https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_0_4/rtl/inc/systemh.inc?revision=37113&view=markup#l105 defined by] the [[System unit|system unit]].
+
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 [https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_0_4/rtl/inc/systemh.inc?revision=37113&view=markup#l105 defined by] the [[System unit|system unit]].
 
It automatically maps to the largest available floating point type available.
 
It automatically maps to the largest available floating point type available.
  
 
{{Data types}}
 
{{Data types}}

Revision as of 15:23, 17 February 2021

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.

Note, that the result of the /-division can be assigned.

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.


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