Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/fundamentals/runtime-libraries/system-double.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The <xref:System.Double> data type stores double-precision floating-point values
| Exponent | 52-62 |
| Sign (0 = Positive, 1 = Negative) | 63 |

Just as decimal fractions are unable to precisely represent some fractional values (such as 1/3 or <xref:System.Math.PI?displayProperty=nameWithType>), binary fractions are unable to represent some fractional values. For example, 1/10, which is represented precisely by .1 as a decimal fraction, is represented by .001100110011 as a binary fraction, with the pattern "0011" repeating to infinity. In this case, the floating-point value provides an imprecise representation of the number that it represents. Performing additional mathematical operations on the original floating-point value often tends to increase its lack of precision. For example, if we compare the result of multiplying .1 by 10 and adding .1 to .1 nine times, we see that addition, because it has involved eight more operations, has produced the less precise result. Note that this disparity is apparent only if we display the two <xref:System.Double> values by using the "R" [standard numeric format string](../../standard/base-types/standard-numeric-format-strings.md), which if necessary displays all 17 digits of precision supported by the <xref:System.Double> type.
Just as decimal fractions are unable to precisely represent some fractional values (such as 1/3 or <xref:System.Math.PI?displayProperty=nameWithType>), binary fractions are unable to represent some fractional values. For example, 1/10, which is represented precisely by .1 as a decimal fraction, is represented by .001100110011 as a binary fraction, with the pattern "0011" repeating to infinity. In this case, the floating-point value provides an imprecise representation of the number that it represents. Performing additional mathematical operations on the original floating-point value often tends to increase its lack of precision. For example, if we compare the result of multiplying .1 by 10 and adding .1 to .1 nine times, we see that addition, because it has involved eight more operations, has produced the less precise result. Note that this disparity is apparent only if we display the two <xref:System.Double> values by using the "R" [standard numeric format string](../../standard/base-types/standard-numeric-format-strings.md), which if necessary displays all 17 digits of precision supported by the <xref:System.Double> type. Starting with .NET 10 (C# 14), the default `ToString()` format also displays all 17 significant digits, so the disparity is visible without using the "R" format specifier.

:::code language="csharp" source="./snippets/System/Double/Overview/csharp/representation1.cs" id="Snippet3":::
:::code language="fsharp" source="./snippets/System/Double/Overview/fsharp/representation1.fs" id="Snippet3":::
Expand Down Expand Up @@ -104,15 +104,15 @@ In cases where a loss of precision is likely to affect the result of a compariso
> [!WARNING]
> <xref:System.Double.Epsilon?displayProperty=nameWithType> is sometimes used as an absolute measure of the distance between two <xref:System.Double> values when testing for equality. However, <xref:System.Double.Epsilon?displayProperty=nameWithType> measures the smallest possible value that can be added to, or subtracted from, a <xref:System.Double> whose value is zero. For most positive and negative <xref:System.Double> values, the value of <xref:System.Double.Epsilon?displayProperty=nameWithType> is too small to be detected. Therefore, except for values that are zero, we do not recommend its use in tests for equality.

The following example uses the latter approach to define an `IsApproximatelyEqual` method that tests the relative difference between two values. It also contrasts the result of calls to the `IsApproximatelyEqual` method and the <xref:System.Double.Equals(System.Double)> method.
The following example uses the latter approach to define an `IsApproximatelyEqual` method that tests the relative difference between two values. The method divides by `Math.Max(value1, value2)` so the comparison is relative to the larger of the two values, which places the result in the correct order of magnitude. If `Math.Max` returns zero (which happens when one value is zero and the other is negative), the method falls back to `Math.Min(value1, value2)` to use the non-zero value as the divisor. It also contrasts the result of calls to the `IsApproximatelyEqual` method and the <xref:System.Double.Equals(System.Double)> method.

:::code language="csharp" source="./snippets/System/Double/Overview/csharp/comparison4.cs" id="Snippet12":::
:::code language="fsharp" source="./snippets/System/Double/Overview/fsharp/comparison4.fs" id="Snippet12":::
:::code language="vb" source="./snippets/System/Double/Overview/vb/comparison4.vb" id="Snippet12":::

## Floating-point values and exceptions

Unlike operations with integral types, which throw exceptions in cases of overflow or illegal operations such as division by zero, operations with floating-point values do not throw exceptions. Instead, in exceptional situations, the result of a floating-point operation is zero, positive infinity, negative infinity, or not a number (NaN):
Unlike operations with integral typeswhich throw a <xref:System.DivideByZeroException> for division by zero, or throw an <xref:System.OverflowException> for overflow in a [checked context](../../csharp/language-reference/statements/checked-and-unchecked.md) (by default in C#, integer overflow doesn't throw)—operations with floating-point values don't throw exceptions. Instead, in exceptional situations, the result of a floating-point operation is zero, positive infinity, negative infinity, or not a number (NaN):

- If the result of a floating-point operation is too small for the destination format, the result is zero. This can occur when two very small numbers are multiplied, as the following example shows.

Expand Down
Loading