Sign in Join
Wiki copytemporary

SetRoundMode: what changed

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

Title: SetRoundMode

OldNewLine
1+With [`math.setRoundMode`](https://www.freepascal.org/docs-html/rtl/math/setroundmode.html) you can set the <abbr title="Floating Point Unit">FPU</abbr>’s method of rounding. If no FPU is present, the global variable [`system.softFloat_rounding_mode`](https://www.freepascal.org/docs-html/rtl/system/softfloat_rounding_mode.html) will determine the rounding mode for floating-point operations implemented by software.
2+
3+This also affects the run-time behavior of the `round` function. (Note, constant expressions can and will be evaluated *during* compile-time, thus the presented techniques do not affect compile-time results.)
4+
5+## invocation
6+
7+`SetRoundMode` resides in the [`math` unit](https://www.freepascal.org/docs-html/rtl/math/index.html). You need to include the `math` unit in the [[Uses|uses clause]].
8+
9+`SetRoundMode` is a unary function. The function’s signature reads:
10+
11+``` pascal
12+function setRoundMode(const roundMode: TFPURoundingMode): TFPURoundingMode
13+```
14+
15+The following values are permissible for `roundMode` (cf. [`TFPURoundingMode`](https://www.freepascal.org/docs-html/rtl/system/tfpuroundingmode.html)):
16+
17+`rmNearest`
18+rounds to the nearest *even* integer (Banker‘s Rounding: `0.5` rounds down to `0`; `1.5` rounds up to `2`). This is the default.
19+
20+`rmDown`
21+generally rounds to the next smaller integer
22+
23+`rmUp`
24+generally rounds to the next larger integer
25+
26+`rmTruncate`
27+cuts off the decimal places
28+
29+The function will return the previous rounding mode, before it was changed. Thus, it returns the same value as [`math.getRoundMode`](https://www.freepascal.org/docs-html/rtl/math/getroundmode.html).
30+
31+> **Warning:** The setting of the rounding mode applies to all *internal* floating point calculations. In particular, it determines how numbers that cannot be represented exactly as the native floating-point type’s values are to be rounded to the internal representation within the scope of the available bits. Therefore, the usage of `setRoundMode` for general rounding purposes is not recommended.
32+
33+## see also
34+
35+- [[Round|round]]
36+- [[Int|int]]
37+- [[Trunc|trunc]]
38+
39+---
40+*From the Free Pascal wiki, page "SetRoundMode", as it stood on 2020-05-21 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).*
41+Category: Pascal

Back to history