Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// <Snippet5>
using System;

public class Example9
{
public static void Main()
{
// <Snippet5>
Double value1 = 1 / 3.0;
Single sValue2 = 1 / 3.0f;
Double value2 = (Double)sValue2;
Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}");

// The example displays the following output:
// 0.3333333333333333 = 0.3333333432674408: False
// </Snippet5>
}
}
// The example displays the following output:
// 0.33333333333333331 = 0.3333333432674408: False
// </Snippet5>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="convert1.fs" />
<Compile Include="convert2.fs" />
<Compile Include="exceptional1.fs" />
<Compile Include="exceptional2.fs" />
<Compile Include="precision1.fs" />
<Compile Include="precisionlist1.fs" />
<Compile Include="precisionlist3.fs" />
<Compile Include="precisionlist4.fs" />
<Compile Include="representation1.fs" />
<Compile Include="representation2.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ let values: obj[] =
for value in values do
let dblValue = value :?> double
printfn $"{value} ({value.GetType().Name}) --> {dblValue:R} ({dblValue.GetType().Name})"

// The example displays the following output:
// 0 (Byte) --> 0 (Double)
// 255 (Byte) --> 255 (Double)
Expand All @@ -36,4 +37,4 @@ for value in values do
// 4294967295 (UInt32) --> 4294967295 (Double)
// 0 (UInt64) --> 0 (Double)
// 18446744073709551615 (UInt64) --> 1.8446744073709552E+19 (Double)
// </Snippet20>
// </Snippet20>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ for value in values do
with :? OverflowException ->
printfn $"Unable to convert {value} to Single."
printfn ""

// The example displays the following output for conversions performed
// in a checked context:
// Unable to convert -1.79769313486232E+308 to Int64.
Expand Down Expand Up @@ -124,4 +125,4 @@ for value in values do
// -Infinity (Double) --> 9223372036854775808 (0x8000000000000000) (UInt64)
// Unable to convert -Infinity to Decimal.
// -Infinity (Double) --> -Infinity (Single)
// </Snippet21>
// </Snippet21>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ let value2 = 9.1642346778e-175
let result = value1 * value2
printfn $"{value1} * {value2} = {result}"
printfn $"{result} = 0: {result.Equals 0.0}"

// The example displays the following output:
// 1.16328759815342E-225 * 9.1642346778E-175 = 0
// 0 = 0: True
// </Snippet1>
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let fromParse = Double.Parse "-4.42330604244772E-305"
printfn $"Double value from literal: {fromLiteral,29:R}"
printfn $"Double value from variable: {fromVariable,28:R}"
printfn $"Double value from Parse method: {fromParse,24:R}"

// On 32-bit versions of the .NET Framework, the output is:
// Double value from literal: -4.42330604244772E-305
// Double value from variable: -4.42330604244772E-305
Expand All @@ -21,4 +22,4 @@ printfn $"Double value from Parse method: {fromParse,24:R}"
// Double value from literal: -4.4233060424477198E-305
// Double value from variable: -4.4233060424477198E-305
// Double value from Parse method: -4.42330604244772E-305
// </Snippet1>
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let sValue2 = 1f /3f

let value2 = double sValue2
printfn $"{value1:R} = {value2:R}: {value1.Equals value2}"

// The example displays the following output:
// 0.33333333333333331 = 0.3333333432674408: False
// 0.3333333333333333 = 0.3333333432674408: False
// </Snippet5>
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ if total.Equals result then
printfn "The sum of the values equals the total."
else
printfn $"The sum of the values ({total}) does not equal the total ({result})."

// The example displays the following output:
// The sum of the values (36.64) does not equal the total (36.64).
//
// If the index items in the Console.WriteLine statement are changed to {0:R},
// the example displays the following output:
// The sum of the values (27.639999999999997) does not equal the total (27.64).
// </Snippet6>
// </Snippet6>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ for i = 1 to 10 do

printfn $".1 * 10: {result1:R}"
printfn $".1 Added 10 times: {result2:R}"

// The example displays the following output:
// .1 * 10: 1
// .1 Added 10 times: 0.99999999999999989
// </Snippet3>
// </Snippet3>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ open System
let value = 123456789012.34567
let additional = Double.Epsilon * 1e15
printfn $"{value} + {additional} = {value + additional}"

// The example displays the following output:
// 123456789012.346 + 4.94065645841247E-309 = 123456789012.346
// </Snippet4>
// </Snippet4>
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
' Visual Basic .NET Document
Option Strict On

' <Snippet5>
Module Example10
Public Sub Main()
' <Snippet5>
Dim value1 As Double = 1 / 3
Dim sValue2 As Single = 1 / 3
Dim value2 As Double = CDbl(sValue2)
Console.WriteLine("{0} = {1}: {2}", value1, value2, value1.Equals(value2))

' The example displays the following output:
' 0.3333333333333333 = 0.3333333432674408: False
' </Snippet5>
End Sub
End Module
' The example displays the following output:
' 0.33333333333333331 = 0.3333333432674408: False
' </Snippet5>

Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
// <Snippet10>
using System;

public class Example1
{
public static void Main()
{
// <Snippet10>
float value1 = 10.201438f;
value1 = (float)Math.Sqrt((float)Math.Pow(value1, 2));
float value2 = (float)Math.Pow((float)value1 * 3.51f, 2);
value2 = ((float)Math.Sqrt(value2)) / 3.51f;
Console.WriteLine($"{value1} = {value2}: {value1.Equals(value2)}");
Console.WriteLine();
Console.WriteLine($"{value1:G9} = {value2:G9}");

// The example displays the following output on .NET:
// 10.201438 = 10.201439: False
// The example displays the following output on .NET Framework:
// 10.20144 = 10.20144: False
// </Snippet10>
}
}
// The example displays the following output:
// 10.20144 = 10.20144: False
//
// 10.201438 = 10.2014389
// </Snippet10>
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// <Snippet11>
using System;

public class Example2
{
public static void Main()
{
// <Snippet11>
float value1 = .3333333f;
float value2 = 1.0f / 3;
int precision = 7;
value1 = (float)Math.Round(value1, precision);
value2 = (float)Math.Round(value2, precision);
Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}");

// The example displays the following output:
// 0.3333333 = 0.3333333: True
// </Snippet11>
}
}
// The example displays the following output:
// 0.3333333 = 0.3333333: True
// </Snippet11>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// <Snippet12>
using System;

public class Example3
{
// <Snippet12>
public static void Main()
{
float one1 = .1f * 10;
Expand All @@ -13,7 +13,7 @@ public static void Main()
Console.WriteLine($"{one1:R} = {one2:R}: {one1.Equals(one2)}");
Console.WriteLine($"{one1:R} is approximately equal to {one2:R}: " +
$"{IsApproximatelyEqual(one1, one2, .000001f)}");

float negativeOne1 = -1 * one1;
float negativeOne2 = -1 * one2;

Expand All @@ -34,16 +34,18 @@ static bool IsApproximatelyEqual(float value1, float value2, float epsilon)
else if (Double.IsInfinity(value2) | Double.IsNaN(value2))
return value1.Equals(value2);

// Handle zero to avoid division by zero
// Handle zero to avoid division by zero.
double divisor = Math.Max(value1, value2);
if (divisor.Equals(0))
divisor = Math.Min(value1, value2);

return Math.Abs((value1 - value2) / divisor) <= epsilon;
}

// The example displays the following output on .NET:
// 1 = 1.0000001: False
// 1 is approximately equal to 1.0000001: True
// -1 = -1.0000001: False
// -1 is approximately equal to -1.0000001: True
// </Snippet12>
}
// The example displays the following output:
// 1 = 1.00000012: False
// 1 is approximately equal to 1.00000012: True
// -1 is approximately equal to -1.00000012: True
// </Snippet12>
Loading
Loading