Trunc: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Trunc
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The [[Standard Pascal]] function [`trunc`](https://www.freepascal.org/docs-html/rtl/system/trunc.html) returns the integer part of a [[Real|real-type value]] rounded toward zero. | |
| 2 | + | ||
| 3 | + | <img src="trunc.png" title="trunc.png" width="100" alt="trunc.png" /> `trunc` is short for “truncate”. The fraction part of the `real` value is, so to speak, cut off. However, unlike the [[Int|int function]] the result is of [[Integer|integer]] type. | |
| 4 | + | ||
| 5 | + | [[FPC]] implements this [[Function|function]] as a compiler function. | |
| 6 | + | ||
| 7 | + | ## definition | |
| 8 | + | ||
| 9 | + | The mathematical definition reads as follows: | |
| 10 | + | ||
| 11 | + | $`\text{trunc}(x) := \begin{cases} | |
| 12 | + | \lfloor x \rfloor & \text{if } x \geq 0 \\ | |
| 13 | + | \lceil x \rceil & \text{if } x < 0 | |
| 14 | + | \end{cases}`$ | |
| 15 | + | ||
| 16 | + | The function `trunc` is defined for all `real` values in the open interval (`low(integer)-1`, `high(integer)+1`). If the supplied parameter is out of range, a program compiled with FPC will stop with the [[runtime error|run-time error]] 207 “Invalid floating point operation”. If the [[sysutils|sysUtils]] is included, this RTE becomes the [`eInvalidOp` exception](https://www.freepascal.org/docs-html/rtl/sysutils/einvalidop.html). | |
| 17 | + | ||
| 18 | + | ## application | |
| 19 | + | ||
| 20 | + | `Trunc` is used to retrieve an integer value. It is primarily used where no loss in accuracy is expected if the fractional part (if any) is removed. | |
| 21 | + | ||
| 22 | + | For example: [[Pascal]] does not have a power operator or function built in. Instead, one has to define their own function doing this task using already existing function. One can utilize the rule $`a^x = e^{x \times \ln a}`$ and the pre-existing `exp` and `ln` functions for that. However, if the operands are integers, it is guaranteed the result will be an integer to, yet `exp` will return a `real` value. The result can be truncated, without loss in precision. For source code, see [[*|asterisk § “exponentiation”]]. | |
| 23 | + | ||
| 24 | + | ## see also | |
| 25 | + | ||
| 26 | + | - [[Round|round]] | |
| 27 | + | - [`math.ceil`](https://www.freepascal.org/docs-html/rtl/math/ceil.html) – round up | |
| 28 | + | - [`math.floor`](https://www.freepascal.org/docs-html/rtl/math/floor.html) – round down | |
| 29 | + | - [[Frac|frac]] – returns the fractional part of a floating point value | |
| 30 | + | - [[Int|int]] – returns the integer part of a floating point value | |
| 31 | + | - [[Div|div]] – integer division | |
| 32 | + | - [[Comparison of approaches for rounding to an integer]] | |
| 33 | + | ||
| 34 | + | --- | |
| 35 | + | *From the Free Pascal wiki, page "Trunc", as it stood on 2020-05-23 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).* |
