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

public class Example5
{
public static void Main()
{
// <Snippet21>
float[] values = { Single.MinValue, -67890.1234f, -12345.6789f,
12345.6789f, 67890.1234f, Single.MaxValue,
Single.NaN, Single.PositiveInfinity,
Expand Down Expand Up @@ -46,98 +46,53 @@ public static void Main()
Console.WriteLine();
}
}

// The example displays the following output for conversions performed
// in a checked context:
// Unable to convert -3.402823E+38 to Int64.
// Unable to convert -3.402823E+38 to UInt64.
// Unable to convert -3.402823E+38 to Decimal.
// -3.402823E+38 (Single) --> -3.40282346638529E+38 (Double)
//
// -67890.13 (Single) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
// Unable to convert -67890.13 to UInt64.
// -67890.13 (Single) --> -67890.12 (Decimal)
// -67890.13 (Single) --> -67890.125 (Double)
//
// -12345.68 (Single) --> -12345 (0xFFFFFFFFFFFFCFC7) (Int64)
// Unable to convert -12345.68 to UInt64.
// -12345.68 (Single) --> -12345.68 (Decimal)
// -12345.68 (Single) --> -12345.6787109375 (Double)
//
// 12345.68 (Single) --> 12345 (0x0000000000003039) (Int64)
// 12345.68 (Single) --> 12345 (0x0000000000003039) (UInt64)
// 12345.68 (Single) --> 12345.68 (Decimal)
// 12345.68 (Single) --> 12345.6787109375 (Double)
//
// 67890.13 (Single) --> 67890 (0x0000000000010932) (Int64)
// 67890.13 (Single) --> 67890 (0x0000000000010932) (UInt64)
// 67890.13 (Single) --> 67890.12 (Decimal)
// 67890.13 (Single) --> 67890.125 (Double)
//
// Unable to convert 3.402823E+38 to Int64.
// Unable to convert 3.402823E+38 to UInt64.
// Unable to convert 3.402823E+38 to Decimal.
// 3.402823E+38 (Single) --> 3.40282346638529E+38 (Double)
//
// Unable to convert NaN to Int64.
// Unable to convert NaN to UInt64.
// Unable to convert NaN to Decimal.
// NaN (Single) --> NaN (Double)
//
// Unable to convert ∞ to Int64.
// Unable to convert ∞ to UInt64.
// Unable to convert ∞ to Decimal.
// ∞ (Single) --> ∞ (Double)
//
// Unable to convert -∞ to Int64.
// Unable to convert -∞ to UInt64.
// Unable to convert -∞ to Decimal.
// -∞ (Single) --> -∞ (Double)
// </Snippet21>
}
}
// The example displays the following output for conversions performed
// in a checked context:
// Unable to convert -3.402823E+38 to Int64.
// Unable to convert -3.402823E+38 to UInt64.
// Unable to convert -3.402823E+38 to Decimal.
// -3.402823E+38 (Single) --> -3.40282346638529E+38 (Double)
//
// -67890.13 (Single) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
// Unable to convert -67890.13 to UInt64.
// -67890.13 (Single) --> -67890.12 (Decimal)
// -67890.13 (Single) --> -67890.125 (Double)
//
// -12345.68 (Single) --> -12345 (0xFFFFFFFFFFFFCFC7) (Int64)
// Unable to convert -12345.68 to UInt64.
// -12345.68 (Single) --> -12345.68 (Decimal)
// -12345.68 (Single) --> -12345.6787109375 (Double)
//
// 12345.68 (Single) --> 12345 (0x0000000000003039) (Int64)
// 12345.68 (Single) --> 12345 (0x0000000000003039) (UInt64)
// 12345.68 (Single) --> 12345.68 (Decimal)
// 12345.68 (Single) --> 12345.6787109375 (Double)
//
// 67890.13 (Single) --> 67890 (0x0000000000010932) (Int64)
// 67890.13 (Single) --> 67890 (0x0000000000010932) (UInt64)
// 67890.13 (Single) --> 67890.12 (Decimal)
// 67890.13 (Single) --> 67890.125 (Double)
//
// Unable to convert 3.402823E+38 to Int64.
// Unable to convert 3.402823E+38 to UInt64.
// Unable to convert 3.402823E+38 to Decimal.
// 3.402823E+38 (Single) --> 3.40282346638529E+38 (Double)
//
// Unable to convert NaN to Int64.
// Unable to convert NaN to UInt64.
// Unable to convert NaN to Decimal.
// NaN (Single) --> NaN (Double)
//
// Unable to convert Infinity to Int64.
// Unable to convert Infinity to UInt64.
// Unable to convert Infinity to Decimal.
// Infinity (Single) --> Infinity (Double)
//
// Unable to convert -Infinity to Int64.
// Unable to convert -Infinity to UInt64.
// Unable to convert -Infinity to Decimal.
// -Infinity (Single) --> -Infinity (Double)
// The example displays the following output for conversions performed
// in an unchecked context:
// -3.402823E+38 (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
// -3.402823E+38 (Single) --> 9223372036854775808 (0x8000000000000000) (UInt64)
// Unable to convert -3.402823E+38 to Decimal.
// -3.402823E+38 (Single) --> -3.40282346638529E+38 (Double)
//
// -67890.13 (Single) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
// -67890.13 (Single) --> 18446744073709483726 (0xFFFFFFFFFFFEF6CE) (UInt64)
// -67890.13 (Single) --> -67890.12 (Decimal)
// -67890.13 (Single) --> -67890.125 (Double)
//
// -12345.68 (Single) --> -12345 (0xFFFFFFFFFFFFCFC7) (Int64)
// -12345.68 (Single) --> 18446744073709539271 (0xFFFFFFFFFFFFCFC7) (UInt64)
// -12345.68 (Single) --> -12345.68 (Decimal)
// -12345.68 (Single) --> -12345.6787109375 (Double)
//
// 12345.68 (Single) --> 12345 (0x0000000000003039) (Int64)
// 12345.68 (Single) --> 12345 (0x0000000000003039) (UInt64)
// 12345.68 (Single) --> 12345.68 (Decimal)
// 12345.68 (Single) --> 12345.6787109375 (Double)
//
// 67890.13 (Single) --> 67890 (0x0000000000010932) (Int64)
// 67890.13 (Single) --> 67890 (0x0000000000010932) (UInt64)
// 67890.13 (Single) --> 67890.12 (Decimal)
// 67890.13 (Single) --> 67890.125 (Double)
//
// 3.402823E+38 (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
// 3.402823E+38 (Single) --> 0 (0x0000000000000000) (UInt64)
// Unable to convert 3.402823E+38 to Decimal.
// 3.402823E+38 (Single) --> 3.40282346638529E+38 (Double)
//
// NaN (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
// NaN (Single) --> 0 (0x0000000000000000) (UInt64)
// Unable to convert NaN to Decimal.
// NaN (Single) --> NaN (Double)
//
// Infinity (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
// Infinity (Single) --> 0 (0x0000000000000000) (UInt64)
// Unable to convert Infinity to Decimal.
// Infinity (Single) --> Infinity (Double)
//
// -Infinity (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
// -Infinity (Single) --> 9223372036854775808 (0x8000000000000000) (UInt64)
// Unable to convert -Infinity to Decimal.
// -Infinity (Single) --> -Infinity (Double)
// </Snippet21>
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// <Snippet1>
using System;

public class Example6
{
public static void Main()
{
// <Snippet1>
float value1 = 1.163287e-36f;
float value2 = 9.164234e-25f;
float result = value1 * value2;
Console.WriteLine($"{value1} * {value2} = {result}");
Console.WriteLine($"{result} = 0: {result.Equals(0.0f)}");

// The example displays the following output:
// 1.163287E-36 * 9.164234E-25 = 0
// 0 = 0: True
// </Snippet1>
}
}
// The example displays the following output:
// 1.163287E-36 * 9.164234E-25 = 0
// 0 = 0: True
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// <Snippet2>
using System;

public class Example7
{
public static void Main()
{
// <Snippet2>
float value1 = 3.065e35f;
float value2 = 6.9375e32f;
float result = value1 * value2;
Expand All @@ -16,13 +16,13 @@ public static void Main()
result = value1 * value2;
Console.WriteLine($"PositiveInfinity: {Single.IsPositiveInfinity(result)}");
Console.WriteLine($"NegativeInfinity: {Single.IsNegativeInfinity(result)}");

// The example displays the following output:
// PositiveInfinity: True
// NegativeInfinity: False
//
// PositiveInfinity: False
// NegativeInfinity: True
// </Snippet2>
}
}

// The example displays the following output:
// PositiveInfinity: True
// NegativeInfinity: False
//
// PositiveInfinity: False
// NegativeInfinity: True
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// <Snippet5>
using System;

public class Example8
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 on .NET:
// 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
@@ -1,7 +1,7 @@
// <Snippet6>
// <Snippet6>
using System;

public class Example9
public class PrecisionList3Example
{
public static void Main()
{
Expand All @@ -17,10 +17,9 @@ public static void Main()
Console.WriteLine($"The sum of the values ({total}) does not equal the total ({result}).");
}
}
// The example displays the following output:
// The sum of the values (27.65) does not equal the total (27.65).
//
// 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.6500015) does not equal the total (27.65).

// The example displays the following output on .NET:
// The sum of the values (27.650002) does not equal the total (27.65).
// The example displays the following output on .NET Framework:
// The sum of the values (27.65) does not equal the total (27.65).
// </Snippet6>
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void Main()
for (int ctr = 0; ctr < values.Length; ctr++)
Console.WriteLine($"{values[ctr]} {(values[ctr].Equals(restoredValues[ctr]) ? "=" : "<>")} {restoredValues[ctr]}");

// The example displays the following output:
// The example displays the following output on .NET Framework:
// 2.882883 <> 2.882883
// 0.3333333 <> 0.3333333
// 3.141593 <> 3.141593
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ public static void Main()
}
// The example displays the following output:
// .2 * 10: 2
// .2 Added 10 times: 2.00000024
// .2 Added 10 times: 2.0000002
// </Snippet3>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static void Main()
Console.WriteLine($"{value} + {additional} = {value + additional}");
}
}

// The example displays the following output:
// 123.456 + 1.401298E-30 = 123.456
// </Snippet4>
Loading