Skip to content

Commit decb396

Browse files
authored
Fix three accuracy issues in System.Double article (#52882)
1 parent 6fd50d2 commit decb396

File tree

14 files changed

+70
-105
lines changed

14 files changed

+70
-105
lines changed

docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public static void Main()
77
{
88
double value1 = .333333333333333;
99
double value2 = 1.0/3;
10-
Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}");
10+
Console.WriteLine($"{value1} = {value2}: {value1.Equals(value2)}");
1111
}
1212
}
1313
// The example displays the following output:

docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison2.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison3.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
// <Snippet11>
21
using System;
32

43
public class Example2
54
{
65
public static void Main()
76
{
7+
// <Snippet11>
88
double value1 = .333333333333333;
99
double value2 = 1.0 / 3;
1010
int precision = 7;
1111
value1 = Math.Round(value1, precision);
1212
value2 = Math.Round(value2, precision);
13-
Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}");
13+
Console.WriteLine($"{value1} = {value2}: {value1.Equals(value2)}");
14+
15+
// The example displays the following output:
16+
// 0.3333333 = 0.3333333: True
17+
// </Snippet11>
1418
}
1519
}
16-
// The example displays the following output:
17-
// 0.3333333 = 0.3333333: True
18-
// </Snippet11>

docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison4.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public static void Main()
1010
for (int ctr = 1; ctr <= 10; ctr++)
1111
one2 += .1;
1212

13-
Console.WriteLine($"{one1:R} = {one2:R}: {one1.Equals(one2)}");
14-
Console.WriteLine($"{one1:R} is approximately equal to {one2:R}: {IsApproximatelyEqual(one1, one2, .000000001)}");
13+
Console.WriteLine($"{one1} = {one2}: {one1.Equals(one2)}");
14+
Console.WriteLine($"{one1} is approximately equal to {one2}: {IsApproximatelyEqual(one1, one2, .000000001)}");
1515
}
1616

1717
static bool IsApproximatelyEqual(double value1, double value2, double epsilon)
@@ -34,6 +34,7 @@ static bool IsApproximatelyEqual(double value1, double value2, double epsilon)
3434
return Math.Abs((value1 - value2) / divisor) <= epsilon;
3535
}
3636
}
37+
3738
// The example displays the following output:
3839
// 1 = 0.99999999999999989: False
3940
// 1 is approximately equal to 0.99999999999999989: True
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="comparison1.fs" />
10+
<Compile Include="comparison3.fs" />
11+
<Compile Include="comparison4.fs" />
12+
<Compile Include="convert1.fs" />
13+
<Compile Include="convert2.fs" />
14+
<Compile Include="exceptional1.fs" />
15+
<Compile Include="exceptional2.fs" />
16+
<Compile Include="precision1.fs" />
17+
<Compile Include="precisionlist1.fs" />
18+
<Compile Include="precisionlist3.fs" />
19+
<Compile Include="precisionlist4.fs" />
20+
<Compile Include="representation1.fs" />
21+
<Compile Include="representation2.fs" />
22+
<Compile Include="source.fs" />
23+
</ItemGroup>
24+
25+
</Project>

docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/fsharp/comparison1.fs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ open System
55

66
let value1 = 0.333333333333333
77
let value2 = 1. / 3.
8-
printfn $"{value1:R} = {value2:R}: {value1.Equals value2}"
8+
printfn $"{value1} = {value2}: {value1.Equals value2}"
9+
910
// The example displays the following output:
1011
// 0.333333333333333 = 0.33333333333333331: False
11-
// </Snippet9>
12+
// </Snippet9>

docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/fsharp/comparison2.fs

Lines changed: 0 additions & 20 deletions
This file was deleted.

docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/fsharp/comparison3.fs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ let v2 = 1. / 3.
88
let precision = 7
99
let value1 = Math.Round(v1, precision)
1010
let value2 = Math.Round(v2, precision)
11-
printfn $"{value1:R} = {value2:R}: {value1.Equals value2}"
11+
printfn $"{value1} = {value2}: {value1.Equals value2}"
12+
1213
// The example displays the following output:
1314
// 0.3333333 = 0.3333333: True
14-
// </Snippet11>
15+
// </Snippet11>

docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/fsharp/comparison4.fs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ open System
55

66
let isApproximatelyEqual (value1: double) (value2: double) (epsilon: double) =
77
// If they are equal anyway, just return True.
8-
if value1.Equals value2 then
8+
if value1.Equals value2 then
99
true
1010
else
1111
// Handle NaN, Infinity.
12-
if Double.IsInfinity value1 || Double.IsNaN value1 then
12+
if Double.IsInfinity value1 || Double.IsNaN value1 then
1313
value1.Equals value2
1414
elif Double.IsInfinity value2 || Double.IsNaN value2 then
1515
value1.Equals value2
1616
else
1717
// Handle zero to avoid division by zero
1818
let divisor = max value1 value2
19-
let divisor =
19+
let divisor =
2020
if divisor.Equals 0 then
2121
min value1 value2
22-
else
22+
else
2323
divisor
2424
abs ((value1 - value2) / divisor) <= epsilon
2525

@@ -28,10 +28,10 @@ let mutable one2 = 0.
2828
for _ = 1 to 10 do
2929
one2 <- one2 + 0.1
3030

31-
printfn $"{one1:R} = {one2:R}: {one1.Equals one2}"
32-
printfn $"{one1:R} is approximately equal to {one2:R}: {isApproximatelyEqual one1 one2 0.000000001}"
31+
printfn $"{one1} = {one2}: {one1.Equals one2}"
32+
printfn $"{one1} is approximately equal to {one2}: {isApproximatelyEqual one1 one2 0.000000001}"
3333

3434
// The example displays the following output:
3535
// 1 = 0.99999999999999989: False
3636
// 1 is approximately equal to 0.99999999999999989: True
37-
// </Snippet12>
37+
// </Snippet12>

docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/vb/comparison1.vb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ Module Example1
66
Public Sub Main()
77
Dim value1 As Double = 0.333333333333333
88
Dim value2 As Double = 1 / 3
9-
Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2))
9+
Console.WriteLine("{0} = {1}: {2}", value1, value2, value1.Equals(value2))
1010
End Sub
1111
End Module
12+
1213
' The example displays the following output:
1314
' 0.333333333333333 = 0.33333333333333331: False
1415
' </Snippet9>

0 commit comments

Comments
 (0)