You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: xml/System/Math.xml
+32-24Lines changed: 32 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -4758,16 +4758,16 @@ Problems of precision in rounding midpoint values are most likely to arise in th
4758
4758
4759
4759
- Define a custom rounding algorithm that performs a "nearly equal" test to determine whether the value to be rounded is acceptably close to a midpoint value. The following example defines a `RoundApproximate` method that examines whether a fractional value is sufficiently near to a midpoint value to be subject to midpoint rounding. As the output from the example shows, it corrects the rounding problem shown in the previous example.
#### Rounding and single-precision floating-point values
4765
4765
4766
4766
The <xref:System.Math.Round%2A> method includes overloads that accept arguments of type <xref:System.Decimal> and <xref:System.Double>. There are no methods that round values of type <xref:System.Single>. If you pass a <xref:System.Single> value to one of the overloads of the <xref:System.Math.Round%2A> method, it is cast (in C#) or converted (in Visual Basic) to a <xref:System.Double>, and the corresponding <xref:System.Math.Round%2A> overload with a <xref:System.Double> parameter is called. Although this is a widening conversion, it often involves a loss of precision, as the following example illustrates. When a <xref:System.Single> value of 16.325 is passed to the <xref:System.Math.Round%2A> method and rounded to two decimal places using the rounding to nearest convention, the result is 16.33 and not the expected result of 16.32.
This unexpected result is due to a loss of precision in the conversion of the <xref:System.Single> value to a <xref:System.Double>. Because the resulting <xref:System.Double> value of 16.325000762939453 is not a midpoint value and is greater than 16.325, it is always rounded upward.
4772
4772
4773
4773
In many cases, as the example illustrates, the loss of precision can be minimized or eliminated by casting or converting the <xref:System.Single> value to a <xref:System.Decimal>. Note that, because this is a narrowing conversion, it requires using a cast operator or calling a conversion method.
@@ -4847,7 +4847,7 @@ This method uses the default rounding convention of <xref:System.MidpointRoundin
The following example demonstrates the <xref:System.Math.Round%28System.Decimal%29> method. Note that the <xref:System.Decimal> value of 4.5 rounds to 4 rather than 5, because this overload uses the default <xref:System.MidpointRounding.ToEven> convention.
4853
4853
@@ -4944,9 +4944,11 @@ The following example demonstrates rounding to the nearest integer value.
4944
4944
</remarks>
4945
4945
<blocksubset="none"type="usage">
4946
4946
<para>Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operations on floating-point values, in some cases the <seecref="M:System.Math.Round(System.Double)" /> method may not appear to round midpoint values to the nearest even integer. In the following example, because the floating-point value .1 has no finite binary representation, the first call to the <seecref="M:System.Math.Round(System.Double)" /> method with a value of 11.5 returns 11 instead of 12.
@@ -5089,9 +5091,9 @@ See [Midpoint values and rounding conventions](#midpoint-values-and-rounding-con
5089
5091
## Example
5090
5092
5091
5093
The following example displays values returned by the <xref:System.Math.Round%28System.Decimal%29> method, the <xref:System.Math.Round%28System.Decimal%2CSystem.MidpointRounding%29> method with a `mode` argument of <xref:System.MidpointRounding.ToEven>, and the <xref:System.Math.Round%28System.Decimal%2CSystem.MidpointRounding%29> method with a `mode` argument of <xref:System.MidpointRounding.AwayFromZero>.
@@ -5176,9 +5178,11 @@ The following example rounds double values with two fractional digits to doubles
5176
5178
<paramrefname="digits" /> is less than 0 or greater than 15.</exception>
5177
5179
<blocksubset="none"type="usage">
5178
5180
<para>Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operations on floating-point values, in some cases the <seecref="M:System.Math.Round(System.Double,System.Int32)" /> method may not appear to round midpoint values to the nearest even value in the <paramrefname="digits" /> decimal position. This is illustrated in the following example, where 2.135 is rounded to 2.13 instead of 2.14. This occurs because internally the method multiplies <paramrefname="value" /> by 10<sup>digits</sup>, and the multiplication operation in this case suffers from a loss of precision.
@@ -5244,8 +5248,8 @@ If the value of the `value` argument is <xref:System.Double.NaN?displayProperty=
5244
5248
## Example
5245
5249
The following example displays values returned by the <xref:System.Math.Round%28System.Double%29> method, the <xref:System.Math.Round%28System.Double%2CSystem.MidpointRounding%29> method with a `mode` argument of <xref:System.MidpointRounding.ToEven>, and the <xref:System.Math.Round%28System.Double%2CSystem.MidpointRounding%29> method with a `mode` argument of <xref:System.MidpointRounding.AwayFromZero>.
@@ -5254,8 +5258,10 @@ The following example displays values returned by the <xref:System.Math.Round%28
5254
5258
<blocksubset="none"type="usage">
5255
5259
<para>Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operations on floating-point values, in some cases the <seecref="M:System.Math.Round(System.Double,System.MidpointRounding)" /> method may not appear to round midpoint values to the nearest even integer. In the following example, because the floating-point value .1 has no finite binary representation, the first call to the <seecref="M:System.Math.Round(System.Double)" /> method with a value of 11.5 returns 11 instead of 12.
@@ -5409,8 +5415,8 @@ If the value of the `value` argument is <xref:System.Double.NaN?displayProperty=
5409
5415
5410
5416
The following example demonstrates how to use the <xref:System.Math.Round%28System.Double%2CSystem.Int32%2CSystem.MidpointRounding%29> method with the <xref:System.MidpointRounding> enumeration.
@@ -5420,9 +5426,11 @@ The following example demonstrates how to use the <xref:System.Math.Round%28Syst
5420
5426
<paramrefname="mode" /> is not a valid value of <seecref="T:System.MidpointRounding" />.</exception>
5421
5427
<blocksubset="none"type="usage">
5422
5428
<para>Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operations on floating-point values, in some cases the <seecref="M:System.Math.Round(System.Double,System.Int32,System.MidpointRounding)" /> method may not appear to round midpoint values as specified by the <paramrefname="mode" /> parameter. This is illustrated in the following example, where 2.135 is rounded to 2.13 instead of 2.14. This occurs because internally the method multiplies <paramrefname="value" /> by 10<sup>digits</sup>, and the multiplication operation in this case suffers from a loss of precision.
0 commit comments