SetRoundMode: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 1 | + | With [`math.setRoundMode`](https://www.freepascal.org/docs-html/rtl/math/setroundmode.html) you can set the FPU’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 | 2 | ||
| 3 | 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 | 4 | ||
| 5 | 5 | ## invocation | |
| 6 | 6 | ||
| 7 | 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 | 8 | ||
| 9 | 9 | `SetRoundMode` is a unary function. The function’s signature reads: | |
| 10 | 10 | ||
| 11 | 11 | ``` pascal | |
| 12 | 12 | function setRoundMode(const roundMode: TFPURoundingMode): TFPURoundingMode | |
| 13 | 13 | ``` | |
| 14 | 14 | ||
| 15 | 15 | The following values are permissible for `roundMode` (cf. [`TFPURoundingMode`](https://www.freepascal.org/docs-html/rtl/system/tfpuroundingmode.html)): | |
| 16 | 16 | ||
| 17 | 17 | `rmNearest` | |
| 18 | 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 | 19 | ||
| 20 | 20 | `rmDown` | |
| 21 | 21 | generally rounds to the next smaller integer | |
| 22 | 22 | ||
| 23 | 23 | `rmUp` | |
| 24 | 24 | generally rounds to the next larger integer | |
| 25 | 25 | ||
| 26 | 26 | `rmTruncate` | |
| 27 | 27 | cuts off the decimal places | |
| 28 | 28 | ||
| 29 | 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 | 30 | ||
| 31 | 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 | 32 | ||
| 33 | 33 | ## see also | |
| 34 | 34 | ||
| 35 | 35 | - [[Round|round]] | |
| 36 | 36 | - [[Int|int]] | |
| 37 | 37 | - [[Trunc|trunc]] | |
| 38 | 38 | ||
| 39 | 39 | --- | |
| 40 | 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 | 41 | Category: Pascal |
