Difference between revisions of "Real"

From Lazarus wiki
Jump to navigationJump to search
(partially undo revision 01CC7B by Bart: we should stress that not all real numbers can be stored; resolve revision 024E45 by Alextp; remove non-standard example of `0.` [real literal without at least one digit after the decimal point]; add →‎see also: Fractions)
 
(3 intermediate revisions by one other user not shown)
Line 2: Line 2:
  
 
<syntaxhighlight lang="pascal" inline>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.
 +
Despite its name, the data type <syntaxhighlight lang="pascal" inline>real</syntaxhighlight> only provides a “reasonable approximation” of ℝ, the set of real numbers.
 +
For example the real number <math>\sqrt{2}</math> may have the <syntaxhighlight lang="pascal" inline>real</syntaxhighlight> value of <syntaxhighlight lang="pascal" inline>1.4</syntaxhighlight> in Pascal.
  
 
== usage ==
 
== usage ==
Line 12: Line 14:
  
 
begin
 
begin
r := 0.; // r becomes zero
+
r := 0.0; { r becomes zero }
 
writeLn(r, ' ', r:8:4);
 
writeLn(r, ' ', r:8:4);
 
 
r := 1e2; // r becomes 1*(10^2) [a hundred]
+
r := 1e2; { r becomes 1*(10^2) [a hundred] }
 
writeLn(r, ' ', r:8:4);
 
writeLn(r, ' ', r:8:4);
 
 
r := r / r; // r becomes one
+
r := r / r; { r becomes one }
 
writeLn(r, ' ', r:8:4);
 
writeLn(r, ' ', r:8:4);
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
Note, that the result of the [[Slash#division|<syntaxhighlight lang="pascal" inline>/</syntaxhighlight>-division]] can be assigned.
+
<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.
  
 
== internal representation ==
 
== internal representation ==
Line 34: Line 36:
  
 
== alternatives ==
 
== 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 [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 {{gitlab|repository|FPC|release_3_2_0/rtl/inc/systemh.inc#L123-332|defined by}} the [[System unit]].
It automatically maps to the largest available floating point type available: Single, Double or Extended.
+
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>.
 +
 
 +
== see also ==
 +
* [[Fractions]]
  
 
{{Data types}}
 
{{Data types}}

Latest revision as of 16:32, 25 January 2023

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

real is a standard type of the Pascal programming language. Despite its name, the data type real only provides a “reasonable approximation” of ℝ, the set of real numbers. For example the real number [math]\displaystyle{ \sqrt{2} }[/math] may have the real value of 1.4 in Pascal.

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.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.

see also


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