Sign in Join
Wiki copytemporary

Trunc: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11The [[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.
22
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.
3+![](/wiki-images/e/ea/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.
44
55[[FPC]] implements this [[Function|function]] as a compiler function.
66
77## definition
88
99The mathematical definition reads as follows:
1010
1111$`\text{trunc}(x) := \begin{cases}
1212\lfloor x \rfloor & \text{if } x \geq 0 \\
1313\lceil x \rceil & \text{if } x < 0
1414\end{cases}`$
1515
1616The 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).
1717
1818## application
1919
2020`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.
2121
2222For 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”]].
2323
2424## see also
2525
2626- [[Round|round]]
2727- [`math.ceil`](https://www.freepascal.org/docs-html/rtl/math/ceil.html) – round up
2828- [`math.floor`](https://www.freepascal.org/docs-html/rtl/math/floor.html) – round down
2929- [[Frac|frac]] – returns the fractional part of a floating point value
3030- [[Int|int]] – returns the integer part of a floating point value
3131- [[Div|div]] – integer division
3232- [[Comparison of approaches for rounding to an integer]]
3333
3434---
3535*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).*

Back to history