Difference between revisions of "SetRoundMode"

From Free Pascal wiki
Jump to navigationJump to search
m (Added warning template)
(review entire page; add content; add syntaxhighlight)
 
Line 1: Line 1:
 
{{LanguageBar}}
 
{{LanguageBar}}
  
With '''SetRoundMode''' you can set the type of rounding. This is required when calling Round (...).
+
With {{Doc|package=RTL|unit=math|identifier=setroundmode|text=<syntaxhighlight lang="pascal" inline>math.setRoundMode</syntaxhighlight>}} you can set the <abbr title="Floating Point Unit">FPU</abbr>’s method of rounding.
 +
If no FPU is present, the global variable {{Doc|package=RTL|unit=system|identifier=softfloat_rounding_mode|text=<syntaxhighlight lang="pascal" inline>system.softFloat_rounding_mode</syntaxhighlight>}} will determine the rounding mode for floating-point operations implemented by software.
  
Syntax:
+
This also affects the run-time behavior of the <syntaxhighlight lang="pascal" inline>round</syntaxhighlight> function.
 +
(Note, constant expressions can and will be evaluated ''during'' compile-time, thus the presented techniques do not affect compile-time results.)
  
<syntaxhighlight lang="pascal">function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;</syntaxhighlight>
+
== invocation ==
 +
<syntaxhighlight lang="pascal" inline>SetRoundMode</syntaxhighlight> resides in the {{Doc|package=RTL|unit=math|text=<syntaxhighlight lang="pascal" inline>math</syntaxhighlight> unit}}.
 +
You need to include the <syntaxhighlight lang="pascal" inline>math</syntaxhighlight> unit in the [[Uses|<syntaxhighlight lang="pascal" inline>uses</syntaxhighlight> clause]].
  
The following parameters are possible for RoundMode:
+
<syntaxhighlight lang="pascal" inline>SetRoundMode</syntaxhighlight> is a unary function.
 +
The function’s signature reads:
 +
<syntaxhighlight lang="pascal">function setRoundMode(const roundMode: TFPURoundingMode): TFPURoundingMode</syntaxhighlight>
  
* rmNearest: Rounds to the nearest even integer (Banker's Rounding: 0.5 rounds down to 0; 1.5 rounds up to 2).
+
The following values are permissible for <syntaxhighlight lang="pascal" inline>roundMode</syntaxhighlight> (cf. {{Doc|package=RTL|unit=system|identifier=tfpuroundingmode|text=<syntaxhighlight lang="pascal" inline>TFPURoundingMode</syntaxhighlight>}}):
* rmDown: generally rounds to the next smaller integer.
+
; <syntaxhighlight lang="pascal" inline>rmNearest</syntaxhighlight>
* rmUp: generally rounds to the next largest integer.
+
: rounds to the nearest ''even'' integer (Banker‘s Rounding: <syntaxhighlight lang="pascal" inline>0.5</syntaxhighlight>&nbsp;rounds down to&nbsp;<syntaxhighlight lang="pascal" inline>0</syntaxhighlight>; <syntaxhighlight lang="pascal" inline>1.5</syntaxhighlight>&nbsp;rounds up to&nbsp;<syntaxhighlight lang="pascal" inline>2</syntaxhighlight>). This is the default.
* rmTruncate: cuts off the decimal places.
+
; <syntaxhighlight lang="pascal" inline>rmDown</syntaxhighlight>
 +
: generally rounds to the next smaller integer
 +
; <syntaxhighlight lang="pascal" inline>rmUp</syntaxhighlight>
 +
: generally rounds to the next larger integer
 +
; <syntaxhighlight lang="pascal" inline>rmTruncate</syntaxhighlight>
 +
: cuts off the decimal places
  
{{Warning|Warning: The setting of the RoundMode applies to all internal floating point calculations. In particular, it determines how numbers that cannot be represented exactly as single / double / extended values are to be rounded to the internal representation within the scope of the available bits. Therefore, the use of SetRoundMode for general rounding purposes is not recommended.}}
+
The function will return the previous rounding mode, before it was changed.
 +
Thus, it returns the same value as {{Doc|package=RTL|unit=math|identifier=getroundmode|text=<syntaxhighlight lang="pascal" inline>math.getRoundMode</syntaxhighlight>}}.
  
== See also ==
+
{{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 <syntaxhighlight lang="pascal" inline>setRoundMode</syntaxhighlight> for general rounding purposes is not recommended.
 +
}}
  
* [[Round]]
+
== see also ==
* [[Int]]
+
* [[Round|<syntaxhighlight lang="pascal" inline>round</syntaxhighlight>]]
* [[Trunc]]
+
* [[Int|<syntaxhighlight lang="pascal" inline>int</syntaxhighlight>]]
 +
* [[Trunc|<syntaxhighlight lang="pascal" inline>trunc</syntaxhighlight>]]
  
 
[[Category:Pascal]]
 
[[Category:Pascal]]

Latest revision as of 12:12, 21 May 2020

Deutsch (de) English (en)

With math.setRoundMode you can set the FPU’s method of rounding. If no FPU is present, the global variable system.softFloat_rounding_mode will determine the rounding mode for floating-point operations implemented by software.

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.)

invocation

SetRoundMode resides in the math unit. You need to include the math unit in the uses clause.

SetRoundMode is a unary function. The function’s signature reads:

function setRoundMode(const roundMode: TFPURoundingMode): TFPURoundingMode

The following values are permissible for roundMode (cf. TFPURoundingMode):

rmNearest
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.
rmDown
generally rounds to the next smaller integer
rmUp
generally rounds to the next larger integer
rmTruncate
cuts off the decimal places

The function will return the previous rounding mode, before it was changed. Thus, it returns the same value as math.getRoundMode.

Warning-icon.png

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.

see also