Real: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Real
| Old | New | Line | |
|---|---|---|---|
| 1 | + | `real` is a [[Standard type|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 $`\sqrt{2}`$ may have the `real` value of `1.4` in Pascal. | |
| 2 | + | ||
| 3 | + | ## usage | |
| 4 | + | ||
| 5 | + | Floating-point literals can be assigned to `real` variables. | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | program realDemo(input, output, stderr); | |
| 9 | + | ||
| 10 | + | var | |
| 11 | + | r: real; | |
| 12 | + | ||
| 13 | + | begin | |
| 14 | + | r := 0.0; { r becomes zero } | |
| 15 | + | writeLn(r, ' ', r:8:4); | |
| 16 | + | ||
| 17 | + | r := 1e2; { r becomes 1*(10^2) [a hundred] } | |
| 18 | + | writeLn(r, ' ', r:8:4); | |
| 19 | + | ||
| 20 | + | r := r / r; { r becomes one } | |
| 21 | + | writeLn(r, ' ', r:8:4); | |
| 22 | + | end. | |
| 23 | + | ``` | |
| 24 | + | ||
| 25 | + | `real`, like all floating-point types, supports [[Slash|/ division operator]], while integer types support `div`-operator. | |
| 26 | + | ||
| 27 | + | ## internal representation | |
| 28 | + | ||
| 29 | + | The internal representation of the type `real` (i.e. number of bytes and byte ordering) and the resulting range and precision are platform dependent. | |
| 30 | + | ||
| 31 | + | Quote from the “FreePascal Programmer's Manual” (Chapter 8.2.5 [Floating point types](https://www.freepascal.org/docs-html/prog/progsu158.html)): | |
| 32 | + | ||
| 33 | + | > 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|double type]] on processors which support floating point operations, while it maps to the [[Single|single type]] on processors which do not support floating point operations in hardware. | |
| 34 | + | ||
| 35 | + | ## alternatives | |
| 36 | + | ||
| 37 | + | Instead of specifying `real` in your code, write [`ValReal`](https://www.freepascal.org/docs-html/rtl/system/valreal.html), a type alias [defined by](https://gitlab.com/freepascal.org/fpc/source/-/tree/release_3_2_0/rtl/inc/systemh.inc#L123-332) the [[System unit]]. It automatically maps to the largest available floating point type available: `single`, `double` or `extended`. | |
| 38 | + | ||
| 39 | + | ## see also | |
| 40 | + | ||
| 41 | + | - [[Fractions]] | |
| 42 | + | ||
| 43 | + | --- | |
| 44 | + | *From the Free Pascal wiki, page "Real", as it stood on 2023-01-25 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
