From 7b3f2d8ed4a85342761a41ba85ce99cb2209f72b Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Sat, 19 Mar 2022 13:02:28 -0700 Subject: [PATCH 1/2] Single F# snippets --- .../fsharp/System/Double/Overview/fs.fsproj | 10 + .../System/Double/Overview/precisionlist1.fs | 8 + .../System/Single/CompareTo/compareto2.fs | 13 ++ .../System/Single/CompareTo/compareto3.fs | 13 ++ .../fsharp/System/Single/CompareTo/fs.fsproj | 12 ++ .../System/Single/CompareTo/singlesample.fs | 119 +++++++++++ .../Single/Epsilon/SingleEquals_25051.fs | 62 ++++++ .../fsharp/System/Single/Epsilon/epsilon.fs | 17 ++ .../fsharp/System/Single/Epsilon/epsilon1.fs | 44 ++++ .../fsharp/System/Single/Epsilon/fs.fsproj | 12 ++ .../fsharp/System/Single/Equals/equalsabs1.fs | 25 +++ .../System/Single/Equals/equalsoverl.fs | 82 ++++++++ .../fsharp/System/Single/Equals/fs.fsproj | 11 + .../fsharp/System/Single/MaxValue/fs.fsproj | 10 + .../System/Single/MaxValue/maxvalueex.fs | 12 ++ .../fsharp/System/Single/MinValue/fs.fsproj | 10 + .../System/Single/MinValue/minvalueex.fs | 12 ++ snippets/fsharp/System/Single/NaN/fs.fsproj | 11 + snippets/fsharp/System/Single/NaN/nan1.fs | 28 +++ .../fsharp/System/Single/NaN/single.nan4.fs | 37 ++++ .../System/Single/Overview/PrecisionList5a.fs | 26 +++ .../System/Single/Overview/comparison1.fs | 9 + .../System/Single/Overview/comparison2.fs | 17 ++ .../System/Single/Overview/comparison3.fs | 14 ++ .../System/Single/Overview/comparison4.fs | 35 ++++ .../fsharp/System/Single/Overview/convert1.fs | 51 +++++ .../fsharp/System/Single/Overview/convert2.fs | 124 ++++++++++++ .../System/Single/Overview/exceptional1.fs | 12 ++ .../System/Single/Overview/exceptional2.fs | 23 +++ .../fsharp/System/Single/Overview/fs.fsproj | 23 +++ .../System/Single/Overview/precisionlist1.fs | 10 + .../System/Single/Overview/precisionlist3.fs | 20 ++ .../System/Single/Overview/precisionlist4a.fs | 28 +++ .../System/Single/Overview/representation1.fs | 15 ++ .../System/Single/Overview/representation2.fs | 11 + snippets/fsharp/System/Single/Parse/fs.fsproj | 12 ++ snippets/fsharp/System/Single/Parse/parse1.fs | 36 ++++ snippets/fsharp/System/Single/Parse/parse2.fs | 55 +++++ snippets/fsharp/System/Single/Parse/parse3.fs | 83 ++++++++ .../System/Single/ToString/ToString1.fs | 189 ++++++++++++++++++ .../System/Single/ToString/ToString7.fs | 14 ++ .../fsharp/System/Single/ToString/fs.fsproj | 11 + .../fsharp/System/Single/TryParse/fs.fsproj | 10 + .../System/Single/TryParse/tryparse1.fs | 89 +++++++++ xml/System/Single.xml | 58 +++++- 45 files changed, 1521 insertions(+), 2 deletions(-) create mode 100644 snippets/fsharp/System/Double/Overview/fs.fsproj create mode 100644 snippets/fsharp/System/Double/Overview/precisionlist1.fs create mode 100644 snippets/fsharp/System/Single/CompareTo/compareto2.fs create mode 100644 snippets/fsharp/System/Single/CompareTo/compareto3.fs create mode 100644 snippets/fsharp/System/Single/CompareTo/fs.fsproj create mode 100644 snippets/fsharp/System/Single/CompareTo/singlesample.fs create mode 100644 snippets/fsharp/System/Single/Epsilon/SingleEquals_25051.fs create mode 100644 snippets/fsharp/System/Single/Epsilon/epsilon.fs create mode 100644 snippets/fsharp/System/Single/Epsilon/epsilon1.fs create mode 100644 snippets/fsharp/System/Single/Epsilon/fs.fsproj create mode 100644 snippets/fsharp/System/Single/Equals/equalsabs1.fs create mode 100644 snippets/fsharp/System/Single/Equals/equalsoverl.fs create mode 100644 snippets/fsharp/System/Single/Equals/fs.fsproj create mode 100644 snippets/fsharp/System/Single/MaxValue/fs.fsproj create mode 100644 snippets/fsharp/System/Single/MaxValue/maxvalueex.fs create mode 100644 snippets/fsharp/System/Single/MinValue/fs.fsproj create mode 100644 snippets/fsharp/System/Single/MinValue/minvalueex.fs create mode 100644 snippets/fsharp/System/Single/NaN/fs.fsproj create mode 100644 snippets/fsharp/System/Single/NaN/nan1.fs create mode 100644 snippets/fsharp/System/Single/NaN/single.nan4.fs create mode 100644 snippets/fsharp/System/Single/Overview/PrecisionList5a.fs create mode 100644 snippets/fsharp/System/Single/Overview/comparison1.fs create mode 100644 snippets/fsharp/System/Single/Overview/comparison2.fs create mode 100644 snippets/fsharp/System/Single/Overview/comparison3.fs create mode 100644 snippets/fsharp/System/Single/Overview/comparison4.fs create mode 100644 snippets/fsharp/System/Single/Overview/convert1.fs create mode 100644 snippets/fsharp/System/Single/Overview/convert2.fs create mode 100644 snippets/fsharp/System/Single/Overview/exceptional1.fs create mode 100644 snippets/fsharp/System/Single/Overview/exceptional2.fs create mode 100644 snippets/fsharp/System/Single/Overview/fs.fsproj create mode 100644 snippets/fsharp/System/Single/Overview/precisionlist1.fs create mode 100644 snippets/fsharp/System/Single/Overview/precisionlist3.fs create mode 100644 snippets/fsharp/System/Single/Overview/precisionlist4a.fs create mode 100644 snippets/fsharp/System/Single/Overview/representation1.fs create mode 100644 snippets/fsharp/System/Single/Overview/representation2.fs create mode 100644 snippets/fsharp/System/Single/Parse/fs.fsproj create mode 100644 snippets/fsharp/System/Single/Parse/parse1.fs create mode 100644 snippets/fsharp/System/Single/Parse/parse2.fs create mode 100644 snippets/fsharp/System/Single/Parse/parse3.fs create mode 100644 snippets/fsharp/System/Single/ToString/ToString1.fs create mode 100644 snippets/fsharp/System/Single/ToString/ToString7.fs create mode 100644 snippets/fsharp/System/Single/ToString/fs.fsproj create mode 100644 snippets/fsharp/System/Single/TryParse/fs.fsproj create mode 100644 snippets/fsharp/System/Single/TryParse/tryparse1.fs diff --git a/snippets/fsharp/System/Double/Overview/fs.fsproj b/snippets/fsharp/System/Double/Overview/fs.fsproj new file mode 100644 index 00000000000..d56171b24e5 --- /dev/null +++ b/snippets/fsharp/System/Double/Overview/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Double/Overview/precisionlist1.fs b/snippets/fsharp/System/Double/Overview/precisionlist1.fs new file mode 100644 index 00000000000..adf202829eb --- /dev/null +++ b/snippets/fsharp/System/Double/Overview/precisionlist1.fs @@ -0,0 +1,8 @@ +// +let value1 = 1. / 3. +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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/CompareTo/compareto2.fs b/snippets/fsharp/System/Single/CompareTo/compareto2.fs new file mode 100644 index 00000000000..e097f734ffc --- /dev/null +++ b/snippets/fsharp/System/Single/CompareTo/compareto2.fs @@ -0,0 +1,13 @@ +module compareto2 + +// +let value1 = 16.5457f +let operand = 3.8899982f +let value2 = value1 * operand / operand +printfn $"Comparing {value1} and {value2}: {value1.CompareTo value2}\n" +printfn $"Comparing {value1:R} and {value2:R}: {value1.CompareTo value2}" +// The example displays the following output: +// Comparing 16.5457 and 16.5457: -1 +// +// Comparing 16.5457 and 16.545702: -1 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/CompareTo/compareto3.fs b/snippets/fsharp/System/Single/CompareTo/compareto3.fs new file mode 100644 index 00000000000..d541c895f14 --- /dev/null +++ b/snippets/fsharp/System/Single/CompareTo/compareto3.fs @@ -0,0 +1,13 @@ +module compareto3 + +// +let value1 = 16.5457f +let operand = 3.8899982f +let value2 = box (value1 * operand / operand) +printfn $"Comparing {value1} and {value2}: {value1.CompareTo value2}\n" +printfn $"Comparing {value1:R} and {value2:R}: {value1.CompareTo value2}" +// The example displays the following output: +// Comparing 16.5457 and 16.5457: -1 +// +// Comparing 16.5457 and 16.545702: -1 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/CompareTo/fs.fsproj b/snippets/fsharp/System/Single/CompareTo/fs.fsproj new file mode 100644 index 00000000000..aa7c060ff45 --- /dev/null +++ b/snippets/fsharp/System/Single/CompareTo/fs.fsproj @@ -0,0 +1,12 @@ + + + Exe + net6.0 + + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Single/CompareTo/singlesample.fs b/snippets/fsharp/System/Single/CompareTo/singlesample.fs new file mode 100644 index 00000000000..6507ce8233f --- /dev/null +++ b/snippets/fsharp/System/Single/CompareTo/singlesample.fs @@ -0,0 +1,119 @@ +module singlesample + +open System + +[] +let main _ = + // + let s = 4.55f + // + + // + printfn $"A Single is of type {s.GetType()}." + // + + // + let mutable finished = false + while not finished do + printf "Enter a real number: " + let inp = stdin.ReadLine() + try + let s = Single.Parse inp + printfn $"You entered {s}." + finished <- true + with + | :? FormatException -> + printfn "You did not enter a number." + | e -> + printfn "An exception occurred while parsing your response: {e}" + // + + // + if s > Single.MaxValue then + printfn "Your number is larger than a Single." + // + + // + if s < Single.MinValue then + printfn "Your number is smaller than a Single." + // + + // + printfn $"Epsilon, or the permittivity of a vacuum, has value {Single.Epsilon}" + // + + // + let zero = 0f + + // This condition will return false. + if 0f / zero = Single.NaN then + printfn "0 / 0 can be tested with Single.NaN." + else + printfn "0 / 0 cannot be tested with Single.NaN use Single.IsNan() instead." + // + + // + // This will return true. + if Single.IsNaN(0f / zero) then + printfn "Single.IsNan() can determine whether a value is not-a-number." + // + + // + // This will equal Infinity. + printfn $"10.0 minus NegativeInfinity equals {10f - Single.NegativeInfinity}." + // + + // + // This will equal Infinity. + printfn $"PositiveInfinity plus 10.0 equals {Single.PositiveInfinity + 10f}." + // + + // + // This will return "true". + printfn $"IsInfinity(3.0F / 0) == %b{Single.IsInfinity(3f / 0f)}." + // + + // + // This will return true. + printfn $"IsPositiveInfinity(4.0F / 0) == {Single.IsPositiveInfinity(4f / 0f)}." + // + + // + // This will return true. + printfn $"IsNegativeInfinity(-5.0F / 0) == {Single.IsNegativeInfinity(-5f / 0f)}." + // + + // + let a = 500f + // + + // + // The variables point to the same objects. + let mutable obj1: obj = a + let obj2: obj = obj1 + + if Single.ReferenceEquals(obj1, obj2) then + printfn "The variables point to the same Single object." + else + printfn "The variables point to different Single objects." + // + + // + let obj1 = single 450 + + if a.CompareTo obj1 < 0 then + printfn $"{a} is less than {obj1}." + + if a.CompareTo obj1 > 0 then + printfn $"{a} is greater than {obj1}." + + if a.CompareTo obj1 = 0 then + printfn $"{a} equals {obj1}." + // + + // + let obj1 = single 500 + if a.Equals obj1 then + printfn "The value type and reference type values are equal." + // + 0 \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Epsilon/SingleEquals_25051.fs b/snippets/fsharp/System/Single/Epsilon/SingleEquals_25051.fs new file mode 100644 index 00000000000..d9fba4a57bd --- /dev/null +++ b/snippets/fsharp/System/Single/Epsilon/SingleEquals_25051.fs @@ -0,0 +1,62 @@ +module SingleEquals_25021 + +open System + +let compareUsingEquals () = + // + // Initialize two floats with apparently identical values + let float1 = 0.33333f + let float2 = 1f / 3f + // Compare them for equality + printfn $"{float1.Equals float2}" // displays false + // + +let compareApproximateValues () = + // + // Initialize two floats with apparently identical values + let float1 = 0.33333f + let float2 = 1f / 3f + // Define the tolerance for variation in their values + let difference = abs (float1 * 0.0001f) + + // Compare the values + // The output to the console indicates that the two values are equal + if abs (float1 - float2) <= difference then + printfn "float1 and float2 are equal." + else + printfn "float1 and float2 are unequal." + // + +let compareObjectsUsingEquals () = + // + // Initialize two floats with apparently identical values + let float1 = 0.33333f + let float2 = box (1f / 3f) + // Compare them for equality + printfn $"{float1.Equals float2}" // displays false + // + +let compareApproximateObjectValues () = + // + // Initialize two floats with apparently identical values + let float1 = 0.33333f + let float2 = box (1f / 3f) + // Define the tolerance for variation in their values + let difference = abs (float1 * 0.0001f) + + // Compare the values + // The output to the console indicates that the two values are equal + if abs (float1 - (float2 :?> float32)) <= difference then + printfn "float1 and float2 are equal." + else + printfn "float1 and float2 are unequal." + // + +compareUsingEquals () +printfn "" +compareApproximateValues () +printfn "" +compareObjectsUsingEquals () +printfn "" +compareApproximateObjectValues () +printfn "" \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Epsilon/epsilon.fs b/snippets/fsharp/System/Single/Epsilon/epsilon.fs new file mode 100644 index 00000000000..ef10b674b18 --- /dev/null +++ b/snippets/fsharp/System/Single/Epsilon/epsilon.fs @@ -0,0 +1,17 @@ +module epsilon + +// +open System + +let values = [ 0f; Single.Epsilon; Single.Epsilon * 0.5f ] + +for i = 0 to values.Length - 2 do + for i2 = i + 1 to values.Length - 1 do + printfn $"{values[i]:r} = {values[i2]:r}: {values[i].Equals(values[i2])}" + printfn "" +// The example displays the following output: +// 0 = 1.401298E-45: False +// 0 = 0: True +// +// 1.401298E-45 = 0: False +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Epsilon/epsilon1.fs b/snippets/fsharp/System/Single/Epsilon/epsilon1.fs new file mode 100644 index 00000000000..71b536519fc --- /dev/null +++ b/snippets/fsharp/System/Single/Epsilon/epsilon1.fs @@ -0,0 +1,44 @@ +module epsilon1 + +// +open System + +let getComponentParts (value: float32) = + let result = $"{value:R}: " + let indent = result.Length + + // Convert the single to a 4-byte array. + let bytes = BitConverter.GetBytes value + let formattedSingle = BitConverter.ToInt32(bytes, 0) + + // Get the sign bit (byte 3, bit 7). + let result = result + $"""Sign: {if formattedSingle >>> 31 <> 0 then "1 (-)" else "0 (+)"}\n""" + + // Get the exponent (byte 2 bit 7 to byte 3, bits 6) + let exponent = (formattedSingle >>> 23) &&& 0x000000FF + let adjustment = if exponent <> 0 then 127 else 126 + let result = result + $"{String(' ', indent)}Exponent: 0x{1:X4} ({exponent - adjustment})\n" + + // Get the significand (bits 0-22) + let significand = + if exponent <> 0 then + (formattedSingle &&& 0x007FFFFF) ||| 0x800000 + else + formattedSingle &&& 0x007FFFFF + + result + $"{String(' ', indent)}Mantissa: 0x{significand:X13}\n" + + +let values = [ 0f; Single.Epsilon ] +for value in values do + printfn $"{getComponentParts value}\n" +// // The example displays the following output: +// 0: Sign: 0 (+) +// Exponent: 0xFFFFFF82 (-126) +// Mantissa: 0x0000000000000 +// +// +// 1.401298E-45: Sign: 0 (+) +// Exponent: 0xFFFFFF82 (-126) +// Mantissa: 0x0000000000001 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Epsilon/fs.fsproj b/snippets/fsharp/System/Single/Epsilon/fs.fsproj new file mode 100644 index 00000000000..4902b409ee9 --- /dev/null +++ b/snippets/fsharp/System/Single/Epsilon/fs.fsproj @@ -0,0 +1,12 @@ + + + Exe + net6.0 + + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Equals/equalsabs1.fs b/snippets/fsharp/System/Single/Equals/equalsabs1.fs new file mode 100644 index 00000000000..ac23fbafaff --- /dev/null +++ b/snippets/fsharp/System/Single/Equals/equalsabs1.fs @@ -0,0 +1,25 @@ +// +open System + +let hasMinimalDifference (value1: float32) (value2: float32) units = + let bytes = BitConverter.GetBytes value1 + let iValue1 = BitConverter.ToInt32(bytes, 0) + let bytes = BitConverter.GetBytes(value2) + let iValue2 = BitConverter.ToInt32(bytes, 0) + + // If the signs are different, return false except for +0 and -0. + if (iValue1 >>> 31) <> (iValue2 >>> 31) then + value1 = value2 + else + let diff = abs (iValue1 - iValue2) + diff <= units + +let value1 = 0.1f * 10f +let value2 = + List.replicate 10 0.1f + |> List.sum + +printfn $"{value1:R} = {value2:R}: {hasMinimalDifference value1 value2 1}" +// The example displays the following output: +// 1 = 1.0000001: True +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Equals/equalsoverl.fs b/snippets/fsharp/System/Single/Equals/equalsoverl.fs new file mode 100644 index 00000000000..5ef81e6ecba --- /dev/null +++ b/snippets/fsharp/System/Single/Equals/equalsoverl.fs @@ -0,0 +1,82 @@ +module equalsoverl + +// +let value = 112f + +let testObjectForEquality (obj: obj) = + printfn $"{value} ({value.GetType().Name}) = {obj} ({obj.GetType().Name}): {value.Equals obj}\n" + +[] +let main _ = + let byte1= 112uy + printfn $"value = byte1: {value.Equals byte1,16}" + testObjectForEquality byte1 + + let short1 = 112s + printfn $"value = short1: {value.Equals short1,16}" + testObjectForEquality short1 + + let int1 = 112 + printfn $"value = int1: {value.Equals int1,18}" + testObjectForEquality int1 + + let long1 = 112L + printfn $"value = long1: {value.Equals long1,17}" + testObjectForEquality long1 + + let sbyte1 = 112y + printfn $"value = sbyte1: {value.Equals sbyte1,16}" + testObjectForEquality sbyte1 + + let ushort1 = 112us + printfn $"value = ushort1: {value.Equals ushort1,16}" + testObjectForEquality ushort1 + + let uint1 = 112u + printfn $"value = uint1: {value.Equals uint1,18}" + testObjectForEquality uint1 + + let ulong1 = 112uL + printfn $"value = ulong1: {value.Equals ulong1,17}" + testObjectForEquality ulong1 + + let dec1 = 112m + printfn $"value = dec1: {value.Equals dec1,21}" + testObjectForEquality dec1 + + let dbl1 = 112. + printfn $"value = dbl1: {value.Equals dbl1,20}" + testObjectForEquality dbl1 + 0 + +// The example displays the following output: +// value = byte1: True +// 112 (Single) = 112 (Byte): False +// +// value = short1: True +// 112 (Single) = 112 (Int16): False +// +// value = int1: True +// 112 (Single) = 112 (Int32): False +// +// value = long1: True +// 112 (Single) = 112 (Int64): False +// +// value = sbyte1: True +// 112 (Single) = 112 (SByte): False +// +// value = ushort1: True +// 112 (Single) = 112 (UInt16): False +// +// value = uint1: True +// 112 (Single) = 112 (UInt32): False +// +// value = ulong1: True +// 112 (Single) = 112 (UInt64): False +// +// value = dec1: False +// 112 (Single) = 112 (Decimal): False +// +// value = dbl1: False +// 112 (Single) = 112 (Double): False +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Equals/fs.fsproj b/snippets/fsharp/System/Single/Equals/fs.fsproj new file mode 100644 index 00000000000..13d901601f1 --- /dev/null +++ b/snippets/fsharp/System/Single/Equals/fs.fsproj @@ -0,0 +1,11 @@ + + + Exe + net6.0 + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Single/MaxValue/fs.fsproj b/snippets/fsharp/System/Single/MaxValue/fs.fsproj new file mode 100644 index 00000000000..64c8db4a187 --- /dev/null +++ b/snippets/fsharp/System/Single/MaxValue/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Single/MaxValue/maxvalueex.fs b/snippets/fsharp/System/Single/MaxValue/maxvalueex.fs new file mode 100644 index 00000000000..fb5b55e2589 --- /dev/null +++ b/snippets/fsharp/System/Single/MaxValue/maxvalueex.fs @@ -0,0 +1,12 @@ +// +open System + +let result1 = 1.867e38f + 2.385e38f +printfn $"{result1} (Positive Infinity: {Single.IsPositiveInfinity result1})" + +let result2 = 1.5935e25f * 7.948e20f +printfn $"{result2} (Positive Infinity: {Single.IsPositiveInfinity result2})" +// The example displays the following output: +// Infinity (Positive Infinity: True) +// Infinity (Positive Infinity: True) +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/MinValue/fs.fsproj b/snippets/fsharp/System/Single/MinValue/fs.fsproj new file mode 100644 index 00000000000..e1c96dbb0a0 --- /dev/null +++ b/snippets/fsharp/System/Single/MinValue/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Single/MinValue/minvalueex.fs b/snippets/fsharp/System/Single/MinValue/minvalueex.fs new file mode 100644 index 00000000000..ceafbb8aef2 --- /dev/null +++ b/snippets/fsharp/System/Single/MinValue/minvalueex.fs @@ -0,0 +1,12 @@ +// +open System + +let result1 = -8.997e37f + -2.985e38f +printfn $"{result1} (Negative Infinity: {Single.IsNegativeInfinity result1})" + +let result2 = -1.5935e25f * 7.948e32f +printfn $"{result2} (Negative Infinity: {Single.IsNegativeInfinity result2})" +// The example displays the following output: +// -Infinity (Negative Infinity: True) +// -Infinity (Negative Infinity: True) +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/NaN/fs.fsproj b/snippets/fsharp/System/Single/NaN/fs.fsproj new file mode 100644 index 00000000000..ad3bef9cee6 --- /dev/null +++ b/snippets/fsharp/System/Single/NaN/fs.fsproj @@ -0,0 +1,11 @@ + + + Exe + net6.0 + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Single/NaN/nan1.fs b/snippets/fsharp/System/Single/NaN/nan1.fs new file mode 100644 index 00000000000..37cbcd3487f --- /dev/null +++ b/snippets/fsharp/System/Single/NaN/nan1.fs @@ -0,0 +1,28 @@ +module nan1 + +open System + +// +let zero = 0f +printfn $"{zero} / {zero} = {zero / zero}" +// The example displays the following output: +// 0 / 0 = NaN +// + +// +let nan1 = Single.NaN + +printfn $"{3} + {nan1} = {3f + nan1}" +printfn $"Abs({nan1}) = {abs nan1}" +// The example displays the following output: +// 3 + NaN = NaN +// Abs(NaN) = NaN +// +Console.WriteLine() + +// +let result = Single.NaN +printfn $"{result} = Single.NaN: {result = Single.NaN}" +// The example displays the following output: +// NaN = Single.Nan: False +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/NaN/single.nan4.fs b/snippets/fsharp/System/Single/NaN/single.nan4.fs new file mode 100644 index 00000000000..34200b4dc3a --- /dev/null +++ b/snippets/fsharp/System/Single/NaN/single.nan4.fs @@ -0,0 +1,37 @@ +module nan4 + +// +open System + +printfn $"NaN == NaN: {Single.NaN = Single.NaN}" +printfn $"NaN != NaN: {Single.NaN <> Single.NaN}" +printfn $"NaN.Equals(NaN): {Single.NaN.Equals Single.NaN}" +printfn $"! NaN.Equals(NaN): {not (Single.NaN.Equals Single.NaN)}" +printfn $"IsNaN: {Double.IsNaN Double.NaN}" + +printfn $"\nNaN > NaN: {Single.NaN > Single.NaN}" +printfn $"NaN >= NaN: {Single.NaN >= Single.NaN}" +printfn $"NaN < NaN: {Single.NaN < Single.NaN}" +printfn $"NaN < 100.0: {Single.NaN < 100f}" +printfn $"NaN <= 100.0: {Single.NaN <= 100f}" +printfn $"NaN >= 100.0: {Single.NaN > 100f}" +printfn $"NaN.CompareTo(NaN): {Single.NaN.CompareTo Single.NaN}" +printfn $"NaN.CompareTo(100.0): {Single.NaN.CompareTo 100f}" +printfn $"(100.0).CompareTo(Single.NaN): {100f.CompareTo Single.NaN}" +// The example displays the following output: +// NaN == NaN: False +// NaN != NaN: True +// NaN.Equals(NaN): True +// ! NaN.Equals(NaN): False +// IsNaN: True +// +// NaN > NaN: False +// NaN >= NaN: False +// NaN < NaN: False +// NaN < 100.0: False +// NaN <= 100.0: False +// NaN >= 100.0: False +// NaN.CompareTo(NaN): 0 +// NaN.CompareTo(100.0): -1 +// (100.0).CompareTo(Single.NaN): 1 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Overview/PrecisionList5a.fs b/snippets/fsharp/System/Single/Overview/PrecisionList5a.fs new file mode 100644 index 00000000000..f6eed8eab0f --- /dev/null +++ b/snippets/fsharp/System/Single/Overview/PrecisionList5a.fs @@ -0,0 +1,26 @@ +module PrecisionList5a + +// +open System +open System.IO + +let values = [| 3.2f / 1.11f; 1f / 3f; MathF.PI |] + +do + use sw = new StreamWriter(@".\Singles.dat") + for i = 0 to values.Length - 1 do + sw.Write $"""{values[i]:G9}{if i < values.Length - 1 then "|" else ""}""" + + +let restoredValues = + use sr = new StreamReader(@".\Singles.dat") + sr.ReadToEnd().Split '|' + |> Array.map Single.Parse + +for i = 0 to values.Length - 1 do + printfn $"""{values[i]} {if values[i].Equals restoredValues[i] then "=" else "<>"} {restoredValues[i]}""" +// The example displays the following output: +// 2.882883 = 2.882883 +// 0.3333333 = 0.3333333 +// 3.141593 = 3.141593 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Overview/comparison1.fs b/snippets/fsharp/System/Single/Overview/comparison1.fs new file mode 100644 index 00000000000..ea11b42f152 --- /dev/null +++ b/snippets/fsharp/System/Single/Overview/comparison1.fs @@ -0,0 +1,9 @@ +module comparison1 + +// +let value1 = 0.3333333f +let value2 = 1f / 3f +printfn $"{value1:R} = {value2:R}: {value1.Equals value2}" +// The example displays the following output: +// 0.3333333 = 0.333333343: False +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Overview/comparison2.fs b/snippets/fsharp/System/Single/Overview/comparison2.fs new file mode 100644 index 00000000000..9d626dd5446 --- /dev/null +++ b/snippets/fsharp/System/Single/Overview/comparison2.fs @@ -0,0 +1,17 @@ +module comparison2 + +// +let value1 = + 10.201438f ** 2f + |> sqrt + +let value2 = + ((value1 * 3.51f) ** 2f |> sqrt) / 3.51f + +printfn $"{value1} = {value2}: {value1.Equals value2}\n" +printfn $"{value1:G9} = {value2:G9}" +// The example displays the following output: +// 10.20144 = 10.20144: False +// +// 10.201438 = 10.2014389 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Overview/comparison3.fs b/snippets/fsharp/System/Single/Overview/comparison3.fs new file mode 100644 index 00000000000..b1d8e90674e --- /dev/null +++ b/snippets/fsharp/System/Single/Overview/comparison3.fs @@ -0,0 +1,14 @@ +module comparison3 + +// +open System + +let value1 = 0.3333333f +let value2 = 1f / 3f +let precision = 7 +let value1r = Math.Round(float value1, precision) |> float32 +let value2r = Math.Round(float value2, precision) |> float32 +printfn $"{value1:R} = {value2:R}: {value1.Equals value2}" +// The example displays the following output: +// 0.3333333 = 0.3333333: True +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Overview/comparison4.fs b/snippets/fsharp/System/Single/Overview/comparison4.fs new file mode 100644 index 00000000000..8bcc671e48d --- /dev/null +++ b/snippets/fsharp/System/Single/Overview/comparison4.fs @@ -0,0 +1,35 @@ +module comparison4 + +// +open System + +let isApproximatelyEqual value1 value2 epsilon = + // If they are equal anyway, just return True. + if value1.Equals value2 then + true + // Handle NaN, Infinity. + elif Single.IsInfinity value1 || Single.IsNaN value1 then + value1.Equals value2 + elif Single.IsInfinity value2 || Single.IsNaN value2 then + value1.Equals value2 + else + // Handle zero to avoid division by zero + let divisor = max value1 value2 + let divisor = + if divisor.Equals 0 then + min value1 value2 + else divisor + abs (value1 - value2) / divisor <= epsilon + + +let one1 = 0.1f * 10f +let mutable one2 = 0f +for _ = 1 to 10 do + one2 <- one2 + 0.1f + +printfn $"{one1:R} = {one2:R}: {one1.Equals one2}" +printfn $"{one1:R} is approximately equal to {one2:R}: {isApproximatelyEqual one1 one2 0.000001f}" +// The example displays the following output: +// 1 = 1.00000012: False +// 1 is approximately equal to 1.00000012: True +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Overview/convert1.fs b/snippets/fsharp/System/Single/Overview/convert1.fs new file mode 100644 index 00000000000..922ccf416b3 --- /dev/null +++ b/snippets/fsharp/System/Single/Overview/convert1.fs @@ -0,0 +1,51 @@ +module convert1 + +// +open System + +let values: obj list = + [ Byte.MinValue; Byte.MaxValue; Decimal.MinValue + Decimal.MaxValue; Double.MinValue; Double.MaxValue + Int16.MinValue; Int16.MaxValue; Int32.MinValue + Int32.MaxValue; Int64.MinValue; Int64.MaxValue + SByte.MinValue; SByte.MaxValue; UInt16.MinValue + UInt16.MaxValue; UInt32.MinValue; UInt32.MaxValue + UInt64.MinValue; UInt64.MaxValue ] + +for value in values do + let sngValue = + match value with + | :? byte as v -> float32 v + | :? decimal as v -> float32 v + | :? double as v -> float32 v + | :? int16 as v -> float32 v + | :? int as v -> float32 v + | :? int64 as v -> float32 v + | :? int8 as v -> float32 v + | :? uint16 as v -> float32 v + | :? uint as v -> float32 v + | :? uint64 as v -> float32 v + | _ -> raise (NotImplementedException "Unknown Type") + printfn $"{value} ({value.GetType().Name}) --> {sngValue:R} ({sngValue.GetType().Name})" +// The example displays the following output: +// 0 (Byte) --> 0 (Single) +// 255 (Byte) --> 255 (Single) +// -79228162514264337593543950335 (Decimal) --> -7.92281625E+28 (Single) +// 79228162514264337593543950335 (Decimal) --> 7.92281625E+28 (Single) +// -1.79769313486232E+308 (Double) --> -Infinity (Single) +// 1.79769313486232E+308 (Double) --> Infinity (Single) +// -32768 (Int16) --> -32768 (Single) +// 32767 (Int16) --> 32767 (Single) +// -2147483648 (Int32) --> -2.14748365E+09 (Single) +// 2147483647 (Int32) --> 2.14748365E+09 (Single) +// -9223372036854775808 (Int64) --> -9.223372E+18 (Single) +// 9223372036854775807 (Int64) --> 9.223372E+18 (Single) +// -128 (SByte) --> -128 (Single) +// 127 (SByte) --> 127 (Single) +// 0 (UInt16) --> 0 (Single) +// 65535 (UInt16) --> 65535 (Single) +// 0 (UInt32) --> 0 (Single) +// 4294967295 (UInt32) --> 4.2949673E+09 (Single) +// 0 (UInt64) --> 0 (Single) +// 18446744073709551615 (UInt64) --> 1.84467441E+19 (Single) +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Overview/convert2.fs b/snippets/fsharp/System/Single/Overview/convert2.fs new file mode 100644 index 00000000000..5bcf93a4fcd --- /dev/null +++ b/snippets/fsharp/System/Single/Overview/convert2.fs @@ -0,0 +1,124 @@ +module convert2 + +// +open System +open Checked + +let values = + [ Single.MinValue; -67890.1234f; -12345.6789f + 12345.6789f; 67890.1234f; Single.MaxValue + Single.NaN; Single.PositiveInfinity + Single.NegativeInfinity ] + +for value in values do + try + let lValue = int64 value + printfn $"{value} ({value.GetType().Name}) --> {lValue} (0x{lValue:X16}) ({lValue.GetType().Name})" + with :? OverflowException -> + printfn $"Unable to convert {value} to Int64." + try + let ulValue = uint64 value + printfn $"{value} ({value.GetType().Name}) --> {ulValue} (0x{ulValue:X16}) ({ulValue.GetType().Name})" + with :? OverflowException -> + printfn $"Unable to convert {value} to UInt64." + try + let dValue = decimal value + printfn $"{value} ({value.GetType().Name}) --> {dValue} ({dValue.GetType().Name})" + with :? OverflowException -> + printfn $"Unable to convert {value} to Decimal." + + let dblValue = double value + printfn $"{value} ({value.GetType().Name}) --> {dblValue} ({dblValue.GetType().Name})\n" +// 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) +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Overview/exceptional1.fs b/snippets/fsharp/System/Single/Overview/exceptional1.fs new file mode 100644 index 00000000000..f4ff254e403 --- /dev/null +++ b/snippets/fsharp/System/Single/Overview/exceptional1.fs @@ -0,0 +1,12 @@ +module exceptional1 + +// +let value1 = 1.163287e-36f +let value2 = 9.164234e-25f +let result = value1 * value2 +printfn $"{value1} * {value2} = {result}" +printfn $"{result} = 0: {result.Equals(0f)}" +// The example displays the following output: +// 1.163287E-36 * 9.164234E-25 = 0 +// 0 = 0: True +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Overview/exceptional2.fs b/snippets/fsharp/System/Single/Overview/exceptional2.fs new file mode 100644 index 00000000000..58a2e94f3c6 --- /dev/null +++ b/snippets/fsharp/System/Single/Overview/exceptional2.fs @@ -0,0 +1,23 @@ +module exceptional2 + +// +open System + +let value1 = 3.065e35f +let value2 = 6.9375e32f +let result = value1 * value2 +printfn $"PositiveInfinity: {Single.IsPositiveInfinity result}" +printfn $"NegativeInfinity: {Single.IsNegativeInfinity result}\n" + +let value3 = -value1 +let result2 = value3 * value2 +printfn $"PositiveInfinity: {Single.IsPositiveInfinity result}" +printfn $"NegativeInfinity: {Single.IsNegativeInfinity result}" + +// The example displays the following output: +// PositiveInfinity: True +// NegativeInfinity: False +// +// PositiveInfinity: False +// NegativeInfinity: True +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Overview/fs.fsproj b/snippets/fsharp/System/Single/Overview/fs.fsproj new file mode 100644 index 00000000000..981faddf299 --- /dev/null +++ b/snippets/fsharp/System/Single/Overview/fs.fsproj @@ -0,0 +1,23 @@ + + + Exe + net6.0 + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Overview/precisionlist1.fs b/snippets/fsharp/System/Single/Overview/precisionlist1.fs new file mode 100644 index 00000000000..d727f9fbd18 --- /dev/null +++ b/snippets/fsharp/System/Single/Overview/precisionlist1.fs @@ -0,0 +1,10 @@ +module percisionlist1 + +// +let value1 = 1. / 3. +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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Overview/precisionlist3.fs b/snippets/fsharp/System/Single/Overview/precisionlist3.fs new file mode 100644 index 00000000000..a9ed62e7ebc --- /dev/null +++ b/snippets/fsharp/System/Single/Overview/precisionlist3.fs @@ -0,0 +1,20 @@ +module precisionlist3 + +// +let values = [| 10.01f; 2.88f; 2.88f; 2.88f; 9f |] +let result = 27.65f +let mutable total = 0f +for value in values do + total <- total + value + +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 (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). +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Overview/precisionlist4a.fs b/snippets/fsharp/System/Single/Overview/precisionlist4a.fs new file mode 100644 index 00000000000..1893a42186a --- /dev/null +++ b/snippets/fsharp/System/Single/Overview/precisionlist4a.fs @@ -0,0 +1,28 @@ +module precisionlist4a + +// +open System +open System.IO + +let values = [| 3.2f / 1.11f; 1f / 3f; MathF.PI |] + +do + use sw = new StreamWriter(@".\Singles.dat") + for i = 0 to values.Length - 1 do + sw.Write(string values[i]) + if i <> values.Length - 1 then + sw.Write "|" + +let restoredValues = + use sr = new StreamReader(@".\Singles.dat") + sr.ReadToEnd().Split '|' + |> Array.map Single.Parse + +for i = 0 to values.Length - 1 do + printfn $"""{values[i]} {if values[i].Equals restoredValues[i] then "=" else "<>"} {restoredValues[i]}""" + +// The example displays the following output: +// 2.882883 <> 2.882883 +// 0.3333333 <> 0.3333333 +// 3.141593 <> 3.141593 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Overview/representation1.fs b/snippets/fsharp/System/Single/Overview/representation1.fs new file mode 100644 index 00000000000..5a93ebb1e18 --- /dev/null +++ b/snippets/fsharp/System/Single/Overview/representation1.fs @@ -0,0 +1,15 @@ +module representation1 + +// +let value = 0.2f +let result1 = value * 10f +let mutable result2 = 0f +for _ = 1 to 10 do + result2 <- result2 + value + +printfn $".2 * 10: {result1:R}" +printfn $".2 Added 10 times: {result2:R}" +// The example displays the following output: +// .2 * 10: 2 +// .2 Added 10 times: 2.00000024 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Overview/representation2.fs b/snippets/fsharp/System/Single/Overview/representation2.fs new file mode 100644 index 00000000000..88a527637ab --- /dev/null +++ b/snippets/fsharp/System/Single/Overview/representation2.fs @@ -0,0 +1,11 @@ +module representation2 + +// +open System + +let value = 123.456f +let additional = Single.Epsilon * 1e15f +printfn $"{value} + {additional} = {value + additional}" +// The example displays the following output: +// 123.456 + 1.401298E-30 = 123.456 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Parse/fs.fsproj b/snippets/fsharp/System/Single/Parse/fs.fsproj new file mode 100644 index 00000000000..8ab586aa794 --- /dev/null +++ b/snippets/fsharp/System/Single/Parse/fs.fsproj @@ -0,0 +1,12 @@ + + + Exe + net6.0 + + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Parse/parse1.fs b/snippets/fsharp/System/Single/Parse/parse1.fs new file mode 100644 index 00000000000..8c00f26c073 --- /dev/null +++ b/snippets/fsharp/System/Single/Parse/parse1.fs @@ -0,0 +1,36 @@ +module parse1 + +// +open System + +let values = + [| "100"; "(100)"; "-123,456,789"; "123.45e+6" + "+500"; "5e2"; "3.1416"; "600."; "-.123" + "-Infinity"; "-1E-16"; string Double.MaxValue + string Single.MinValue; String.Empty |] + +for value in values do + try + let number = Single.Parse value + printfn $"{value} -> {number}" + with + | :? FormatException -> + printfn $"'{value}' is not in a valid format." + | :? OverflowException -> + printfn $"{value} is outside the range of a Single." +// The example displays the following output: +// 100 -> 100 +// '(100)' is not in a valid format. +// -123,456,789 -> -1.234568E+08 +// 123.45e+6 -> 1.2345E+08 +// +500 -> 500 +// 5e2 -> 500 +// 3.1416 -> 3.1416 +// 600. -> 600 +// -.123 -> -0.123 +// -Infinity -> -Infinity +// -1E-16 -> -1E-16 +// 1.79769313486232E+308 is outside the range of a Single. +// -3.402823E+38 -> -3.402823E+38 +// '' is not in a valid format. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Parse/parse2.fs b/snippets/fsharp/System/Single/Parse/parse2.fs new file mode 100644 index 00000000000..a50ee8b568c --- /dev/null +++ b/snippets/fsharp/System/Single/Parse/parse2.fs @@ -0,0 +1,55 @@ +module parse2 + +// +open System +open System.Globalization +open System.Threading + +let showNumericValue value (styles: NumberStyles) = + try + let number = Single.Parse(value, styles) + printfn $"Converted '{value}' using {styles} to {number}." + with :? FormatException -> + printfn $"Unable to parse '{value}' with styles {styles}." + printfn "" + +[] +let main _ = + // Set current thread culture to en-US. + Thread.CurrentThread.CurrentCulture <- CultureInfo.CreateSpecificCulture "en-US" + + // Parse a string in exponential notation with only the AllowExponent flag. + let value = "-1.063E-02" + let styles = NumberStyles.AllowExponent + showNumericValue value styles + + // Parse a string in exponential notation + // with the AllowExponent and Number flags. + let styles = NumberStyles.AllowExponent ||| NumberStyles.Number + showNumericValue value styles + + // Parse a currency value with leading and trailing white space, and + // white space after the U.S. currency symbol. + let value = " $ 6,164.3299 " + let styles = NumberStyles.Number ||| NumberStyles.AllowCurrencySymbol + showNumericValue value styles + + // Parse negative value with thousands separator and decimal. + let value = "(4,320.64)" + let styles = NumberStyles.AllowParentheses ||| NumberStyles.AllowTrailingSign ||| NumberStyles.Float + showNumericValue value styles + + let styles = NumberStyles.AllowParentheses ||| NumberStyles.AllowTrailingSign ||| NumberStyles.Float ||| NumberStyles.AllowThousands + showNumericValue value styles + 0 +// The example displays the following output to the console: +// Unable to parse '-1.063E-02' with styles AllowExponent. +// +// Converted '-1.063E-02' using AllowTrailingSign, AllowThousands, Float to -0.01063. +// +// Converted ' $ 6,164.3299 ' using Number, AllowCurrencySymbol to 6164.3299. +// +// Unable to parse '(4,320.64)' with styles AllowTrailingSign, AllowParentheses, Float. +// +// Converted '(4,320.64)' using AllowTrailingSign, AllowParentheses, AllowThousands, Float to -4320.64. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/Parse/parse3.fs b/snippets/fsharp/System/Single/Parse/parse3.fs new file mode 100644 index 00000000000..2de2d8c2fb4 --- /dev/null +++ b/snippets/fsharp/System/Single/Parse/parse3.fs @@ -0,0 +1,83 @@ +module parse3 + +// +open System +open System.Globalization + +// Define a list of string values. +let values = + [ " 987.654E-2"; " 987,654E-2"; "(98765,43210)" + "9,876,543.210"; "9.876.543,210"; "98_76_54_32,19" ] +// Create a custom culture based on the invariant culture. +let ci = CultureInfo "" +ci.NumberFormat.NumberGroupSizes <- [| 2 |] +ci.NumberFormat.NumberGroupSeparator <- "_" + +// Define a list of format providers. +let providers = + [ CultureInfo "en-US" + CultureInfo "nl-NL" + ci ] + +// Define a list of styles. +let styles = [ NumberStyles.Currency; NumberStyles.Float ] + +// Iterate the list of format providers. +for provider in providers do + printfn $"""Parsing using the {if provider.Name = String.Empty then "Invariant" else provider.Name} culture:""" + // Parse each element in the array of string values. + for value in values do + for style in styles do + try + let number = Single.Parse(value, style, provider) + printfn $" {value} ({style}) -> {number}" + with + | :? FormatException -> + printfn $" '{value}' is invalid using {style}." + | :? OverflowException -> + printfn $" '{value}' is out of the range of a Single." + printfn "" + +// The example displays the following output: +// Parsing using the en-US culture: +// ' 987.654E-2' is invalid using Currency. +// 987.654E-2 (Float) -> 9.87654 +// ' 987,654E-2' is invalid using Currency. +// ' 987,654E-2' is invalid using Float. +// (98765,43210) (Currency) -> -9.876543E+09 +// '(98765,43210)' is invalid using Float. +// 9,876,543.210 (Currency) -> 9876543 +// '9,876,543.210' is invalid using Float. +// '9.876.543,210' is invalid using Currency. +// '9.876.543,210' is invalid using Float. +// '98_76_54_32,19' is invalid using Currency. +// '98_76_54_32,19' is invalid using Float. +// +// Parsing using the nl-NL culture: +// ' 987.654E-2' is invalid using Currency. +// ' 987.654E-2' is invalid using Float. +// ' 987,654E-2' is invalid using Currency. +// 987,654E-2 (Float) -> 9.87654 +// (98765,43210) (Currency) -> -98765.43 +// '(98765,43210)' is invalid using Float. +// '9,876,543.210' is invalid using Currency. +// '9,876,543.210' is invalid using Float. +// 9.876.543,210 (Currency) -> 9876543 +// '9.876.543,210' is invalid using Float. +// '98_76_54_32,19' is invalid using Currency. +// '98_76_54_32,19' is invalid using Float. +// +// Parsing using the Invariant culture: +// ' 987.654E-2' is invalid using Currency. +// 987.654E-2 (Float) -> 9.87654 +// ' 987,654E-2' is invalid using Currency. +// ' 987,654E-2' is invalid using Float. +// (98765,43210) (Currency) -> -9.876543E+09 +// '(98765,43210)' is invalid using Float. +// 9,876,543.210 (Currency) -> 9876543 +// '9,876,543.210' is invalid using Float. +// '9.876.543,210' is invalid using Currency. +// '9.876.543,210' is invalid using Float. +// 98_76_54_32,19 (Currency) -> 9.876543E+09 +// '98_76_54_32,19' is invalid using Float. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/ToString/ToString1.fs b/snippets/fsharp/System/Single/ToString/ToString1.fs new file mode 100644 index 00000000000..afdfdf371c4 --- /dev/null +++ b/snippets/fsharp/System/Single/ToString/ToString1.fs @@ -0,0 +1,189 @@ +module ToString1 + +open System.Globalization + +let callDefaultToString () = + // + let number = 1.6E20F + // Displays 1.6E+20. + printfn $"{number.ToString()}" + + let number = 1.6E2F + // Displays 160. + printfn $"{number.ToString()}" + + let number = -3.541F + // Displays -3.541. + printfn $"{number.ToString()}" + + let number = -1502345222199E-07F + // Displays -150234.5222199. + printfn $"{number.ToString()}" + + let number = -15023452221990199574E-09F + // Displays -15023452221.9902. + printfn $"{number.ToString()}" + + let number = 0.60344F + // Displays 0.60344. + printfn $"{number.ToString()}" + + let number = 0.000000001F + // Displays 1E-09. + printfn $"{number.ToString()}" + // + +let callToStringWithFormatProvider () = + // + let value = -16325.62015F + // Display value using the invariant culture. + printfn $"{value.ToString CultureInfo.InvariantCulture}" + // Display value using the en-GB culture. + printfn $"""{value.ToString(CultureInfo.CreateSpecificCulture "en-GB")}""" + // Display value using the de-DE culture. + printfn $"""{value.ToString(CultureInfo.CreateSpecificCulture "de-DE")}""" + + let value = 16034.125E21F + // Display value using the invariant culture. + printfn $"{value.ToString CultureInfo.InvariantCulture}" + // Display value using the en-GB culture. + printfn $"""{value.ToString(CultureInfo.CreateSpecificCulture "en-GB")}""" + // Display value using the de-DE culture. + printfn $"""{value.ToString(CultureInfo.CreateSpecificCulture "de-DE")}""" + // This example displays the following output to the console: + // -16325.62015 + // -16325.62015 + // -16325,62015 + // 1.6034125E+25 + // 1.6034125E+25 + // 1,6034125E+25 + // + +let callToStringWithFormatString () = + // + let numbers = + [| 1054.32179F; -195489100.8377F; 1.0437E21F; -1.0573e-05F |] + let specifiers = + [| "C"; "E"; "e"; "F"; "G"; "N"; "P" + "R"; "#,000.000"; "0.###E-000" + "000,000,000,000.00###" |] + + for number in numbers do + printfn $"Formatting of {number}:" + for specifier in specifiers do + printfn $" {specifier,5}: {number.ToString specifier}" + printfn "" + // The example displays the following output to the console: + // Formatting of 1054.32179: + // C: $1,054.32 + // E: 1.054322E+003 + // e: 1.054322e+003 + // F: 1054.32 + // G: 1054.32179 + // N: 1,054.32 + // P: 105,432.18 % + // R: 1054.32179 + // #,000.000: 1,054.322 + // 0.###E-000: 1.054E003 + // 000,000,000,000.00###: 000,000,001,054.322 + // + // Formatting of -195489100.8377: + // C: ($195,489,100.84) + // E: -1.954891E+008 + // e: -1.954891e+008 + // F: -195489100.84 + // G: -195489100.8377 + // N: -195,489,100.84 + // P: -19,548,910,083.77 % + // R: -195489100.8377 + // #,000.000: -195,489,100.838 + // 0.###E-000: -1.955E008 + // 000,000,000,000.00###: -000,195,489,100.00 + // + // Formatting of 1.0437E+21: + // C: $1,043,700,000,000,000,000,000.00 + // E: 1.043700E+021 + // e: 1.043700e+021 + // F: 1043700000000000000000.00 + // G: 1.0437E+21 + // N: 1,043,700,000,000,000,000,000.00 + // P: 104,370,000,000,000,000,000,000.00 % + // R: 1.0437E+21 + // #,000.000: 1,043,700,000,000,000,000,000.000 + // 0.###E-000: 1.044E021 + // 000,000,000,000.00###: 1,043,700,000,000,000,000,000.00 + // + // Formatting of -1.0573E-05: + // C: $0.00 + // E: -1.057300E-005 + // e: -1.057300e-005 + // F: 0.00 + // G: -1.0573E-05 + // N: 0.00 + // P: 0.00 % + // R: -1.0573E-05 + // #,000.000: 000.000 + // 0.###E-000: -1.057E-005 + // 000,000,000,000.00###: -000,000,000,000.00001 + // + +let callToStringWithFormatStringAndProvider () = + // + let value = 16325.62901F + + // Use standard numeric format specifiers. + let specifier = "G" + let culture = CultureInfo.CreateSpecificCulture "eu-ES" + printfn $"{value.ToString(specifier, culture)}" + // Displays: 16325,62901 + printfn $"{value.ToString(specifier, CultureInfo.InvariantCulture)}" + // Displays: 16325.62901 + + let specifier = "C" + let culture = CultureInfo.CreateSpecificCulture "en-US" + printfn $"{value.ToString(specifier, culture)}" + // Displays: $16,325.63 + let culture = CultureInfo.CreateSpecificCulture "en-GB" + printfn $"{value.ToString(specifier, culture)}" + // Displays: £16,325.63 + + let specifier = "E04" + let culture = CultureInfo.CreateSpecificCulture "sv-SE" + printfn $"{value.ToString(specifier, culture)}" + // Displays: 1,6326E+004 + let culture = CultureInfo.CreateSpecificCulture "en-NZ" + printfn $"{value.ToString(specifier, culture)}" + // Displays: 1.6326E+004 + + let specifier = "F" + let culture = CultureInfo.CreateSpecificCulture "fr-FR" + printfn $"{value.ToString(specifier, culture)}" + // Displays: 16325,63 + let culture = CultureInfo.CreateSpecificCulture "en-CA" + printfn $"{value.ToString(specifier, culture)}" + // Displays: 16325.63 + + let specifier = "N" + let culture = CultureInfo.CreateSpecificCulture "es-ES" + printfn $"{value.ToString(specifier, culture)}" + // Displays: 16.325,63 + let culture = CultureInfo.CreateSpecificCulture "fr-CA" + printfn $"{value.ToString(specifier, culture)}" + // Displays: 16 325,63 + + let specifier = "P" + let culture = CultureInfo.InvariantCulture + printfn $"{(value / 10000f).ToString(specifier, culture)}" + // Displays: 163.26 % + let culture = CultureInfo.CreateSpecificCulture "ar-EG" + printfn $"{(value / 10000f).ToString(specifier, culture)}" + // Displays: 163.256 % + // + +callDefaultToString () +printfn "----------" +callToStringWithFormatProvider () +printfn "----------" +callToStringWithFormatString () +printfn "----------" +callToStringWithFormatStringAndProvider () diff --git a/snippets/fsharp/System/Single/ToString/ToString7.fs b/snippets/fsharp/System/Single/ToString/ToString7.fs new file mode 100644 index 00000000000..db15c4b7636 --- /dev/null +++ b/snippets/fsharp/System/Single/ToString/ToString7.fs @@ -0,0 +1,14 @@ +module ToString7 + +// +let number = 1764.3789 + +// Format as a currency value. +printfn $"""{number.ToString "C"}""" + +// Format as a numeric value with 3 decimal places. +printfn $"""{number.ToString "N3"}""" +// The example displays the following output: +// $1,764.38 +// 1,764.379 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Single/ToString/fs.fsproj b/snippets/fsharp/System/Single/ToString/fs.fsproj new file mode 100644 index 00000000000..12a08c6998e --- /dev/null +++ b/snippets/fsharp/System/Single/ToString/fs.fsproj @@ -0,0 +1,11 @@ + + + Exe + net6.0 + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Single/TryParse/fs.fsproj b/snippets/fsharp/System/Single/TryParse/fs.fsproj new file mode 100644 index 00000000000..a355093939c --- /dev/null +++ b/snippets/fsharp/System/Single/TryParse/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Single/TryParse/tryparse1.fs b/snippets/fsharp/System/Single/TryParse/tryparse1.fs new file mode 100644 index 00000000000..52ff4ed3f1e --- /dev/null +++ b/snippets/fsharp/System/Single/TryParse/tryparse1.fs @@ -0,0 +1,89 @@ +open System + +let defaultTryParse () = + // + // Parse a floating-point value with a thousands separator. + let value = "1,643.57" + match Single.TryParse value with + | true, number -> + printfn $"{number}" + | _ -> + printfn $"Unable to parse '{value}'." + + // Parse a floating-point value with a currency symbol and a + // thousands separator. + let value = "$1,643.57" + match Single.TryParse value with + | true, number -> + printfn $"{number}" + | _ -> + printfn $"Unable to parse '{value}'." + + // Parse value in exponential notation. + let value = "-1.643e6" + match Single.TryParse value with + | true, number -> + printfn $"{number}" + | _ -> + printfn $"Unable to parse '{value}'." + + // Parse a negative integer value. + let value = "-168934617882109132" + match Single.TryParse value with + | true, number -> + printfn $"{number}" + | _ -> + printfn $"Unable to parse '{value}'." + // The example displays the following output: + // 1643.57 + // Unable to parse '$1,643.57'. + // -164300 + // -1.689346E+17 + // + +let tryParseWithConstraints () = + // + // Parse currency value using en-GB culture. + let value = "£1,097.63" + let style = System.Globalization.NumberStyles.Number ||| System.Globalization.NumberStyles.AllowCurrencySymbol + let culture = System.Globalization.CultureInfo.CreateSpecificCulture "en-GB" + match Single.TryParse(value, style, culture) with + | true, number -> + printfn $"Converted '{value}' to {number}." + | _ -> + printfn $"Unable to convert '{value}'." + + let value = "1345,978" + let style = System.Globalization.NumberStyles.AllowDecimalPoint + let culture = System.Globalization.CultureInfo.CreateSpecificCulture "fr-FR" + match Single.TryParse(value, style, culture) with + | true, number -> + printfn $"Converted '{value}' to {number}." + | _ -> + printfn $"Unable to convert '{value}'." + + let value = "1.345,978" + let style = System.Globalization.NumberStyles.AllowDecimalPoint ||| System.Globalization.NumberStyles.AllowThousands + let culture = System.Globalization.CultureInfo.CreateSpecificCulture "es-ES" + match Single.TryParse(value, style, culture) with + | true, number -> + printfn $"Converted '{value}' to {number}." + | _ -> + printfn $"Unable to convert '{value}'." + + let value = "1 345,978" + match Single.TryParse(value, style, culture) with + | true, number -> + printfn $"Converted '{value}' to {number}." + | _ -> + printfn $"Unable to convert '{value}'." + // The example displays the following output: + // Converted '£1,097.63' to 1097.63. + // Converted '1345,978' to 1345.978. + // Converted '1.345,978' to 1345.978. + // Unable to convert '1 345,978'. + // + +defaultTryParse () +printfn "----------" +tryParseWithConstraints () \ No newline at end of file diff --git a/xml/System/Single.xml b/xml/System/Single.xml index f8433bafc64..867fcf236b0 100644 --- a/xml/System/Single.xml +++ b/xml/System/Single.xml @@ -271,6 +271,7 @@ Just as decimal fractions are unable to precisely represent some fractional values (such as 1/3 or ), binary fractions are unable to represent some fractional values. For example, 2/10, which is represented precisely by .2 as a decimal fraction, is represented by .0011111001001100 as a binary fraction, with the pattern "1100" 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 increases its lack of precision. For example, if you compare the results of multiplying .3 by 10 and adding .3 to .3 nine times, you will see that addition produces the less precise result, because it involves eight more operations than multiplication. Note that this disparity is apparent only if you display the two values by using the "R" [standard numeric format string](/dotnet/standard/base-types/standard-numeric-format-strings), which, if necessary, displays all 9 digits of precision supported by the type. :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/representation1.cs" interactive="try-dotnet" id="Snippet3"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/representation1.fs" id="Snippet3"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.structure/vb/representation1.vb" id="Snippet3"::: Because some numbers cannot be represented exactly as fractional binary values, floating-point numbers can only approximate real numbers. @@ -278,6 +279,7 @@ All floating-point numbers have a limited number of significant digits, which also determines how accurately a floating-point value approximates a real number. A value has up to 7 decimal digits of precision, although a maximum of 9 digits is maintained internally. This means that some floating-point operations may lack the precision to change a floating-point value. The following example defines a large single-precision floating-point value, and then adds the product of and one quadrillion to it. However, the product is too small to modify the original floating-point value. Its least significant digit is thousandths, whereas the most significant digit in the product is 10-30. :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/representation2.cs" interactive="try-dotnet" id="Snippet4"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/representation2.fs" id="Snippet4"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.structure/vb/representation2.vb" id="Snippet4"::: The limited precision of a floating-point number has several consequences: @@ -285,6 +287,7 @@ - Two floating-point numbers that appear equal for a particular precision might not compare equal because their least significant digits are different. In the following example, a series of numbers are added together, and their total is compared with their expected total. Although the two values appear to be the same, a call to the `Equals` method indicates that they are not. :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/precisionlist3.cs" interactive="try-dotnet" id="Snippet6"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/precisionlist3.fs" id="Snippet6"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.structure/vb/precisionlist3.vb" id="Snippet6"::: If you change the format items in the statement from `{0}` and `{1}` to `{0:R}` and `{1:R}` to display all significant digits of the two values, it is clear that the two values are unequal because of a loss of precision during the addition operations. In this case, the issue can be resolved by calling the method to round the values to the desired precision before performing the comparison. @@ -296,16 +299,19 @@ - A value might not round-trip if a floating-point number is involved. A value is said to round-trip if an operation converts an original floating-point number to another form, an inverse operation transforms the converted form back to a floating-point number, and the final floating-point number is equal to the original floating-point number. The round trip might fail because one or more least significant digits are lost or changed in a conversion. In the following example, three values are converted to strings and saved in a file. As the output shows, although the values appear to be identical, the restored values are not equal to the original values. :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/precisionlist4a.cs" id="Snippet17"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/precisionlist4a.fs" id="Snippet17"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.structure/vb/PrecisionList4a.vb" id="Snippet17"::: In this case, the values can be successfully round-tripped by using the "G9" [standard numeric format string](/dotnet/standard/base-types/standard-numeric-format-strings) to preserve the full precision of values, as the following example shows. :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/PrecisionList5a.cs" id="Snippet18"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/PrecisionList5a.fs" id="Snippet18"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.structure/vb/PrecisionList5a.vb" id="Snippet18"::: - values have less precision than values. A value that is converted to a seemingly equivalent often does not equal the value because of differences in precision. In the following example, the result of identical division operations is assigned to a value and a value. After the value is cast to a , a comparison of the two values shows that they are unequal. :::code language="csharp" source="~/snippets/csharp/System/Double/Overview/precisionlist1.cs" interactive="try-dotnet" id="Snippet5"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Double/Overview/precisionlist1.fs" id="Snippet5"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.double.structure/vb/precisionlist1.vb" id="Snippet5"::: To avoid this problem, either use the data type in place of the data type, or use the method so that both values have the same precision. @@ -315,11 +321,13 @@ To be considered equal, two values must represent identical values. However, because of differences in precision between values, or because of a loss of precision by one or both values, floating-point values that are expected to be identical often turn out to be unequal due to differences in their least significant digits. As a result, calls to the method to determine whether two values are equal, or calls to the method to determine the relationship between two values, often yield unexpected results. This is evident in the following example, where two apparently equal values turn out to be unequal, because the first value has 7 digits of precision, whereas the second value has 9. :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/comparison1.cs" interactive="try-dotnet" id="Snippet9"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/comparison1.fs" id="Snippet9"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.structure/vb/comparison1.vb" id="Snippet9"::: Calculated values that follow different code paths and that are manipulated in different ways often prove to be unequal. In the following example, one value is squared, and then the square root is calculated to restore the original value. A second is multiplied by 3.51 and squared before the square root of the result is divided by 3.51 to restore the original value. Although the two values appear to be identical, a call to the method indicates that they are not equal. Using the "G9" standard format string to return a result string that displays all the significant digits of each value shows that the second value is .0000000000001 less than the first. :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/comparison2.cs" interactive="try-dotnet" id="Snippet10"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/comparison2.fs" id="Snippet10"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.structure/vb/comparison2.vb" id="Snippet10"::: In cases where a loss of precision is likely to affect the result of a comparison, you can use the following techniques instead of calling the or method: @@ -327,6 +335,7 @@ - Call the method to ensure that both values have the same precision. The following example modifies a previous example to use this approach so that two fractional values are equivalent. :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/comparison3.cs" interactive="try-dotnet" id="Snippet11"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/comparison3.fs" id="Snippet11"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.structure/vb/comparison3.vb" id="Snippet11"::: The problem of precision still applies to rounding of midpoint values. For more information, see the method. @@ -339,6 +348,7 @@ 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 method. :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/comparison4.cs" interactive="try-dotnet" id="Snippet12"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/comparison4.fs" id="Snippet12"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.structure/vb/comparison4.vb" id="Snippet12"::: @@ -348,11 +358,13 @@ - 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 floating-point numbers are multiplied, as the following example shows. :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/exceptional1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/exceptional1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.structure/vb/exceptional1.vb" id="Snippet1"::: - If the magnitude of the result of a floating-point operation exceeds the range of the destination format, the result of the operation is or , as appropriate for the sign of the result. The result of an operation that overflows is , and the result of an operation that overflows is , as the following example shows. :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/exceptional2.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/exceptional2.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.structure/vb/exceptional2.vb" id="Snippet2"::: also results from a division by zero with a positive dividend, and results from a division by zero with a negative dividend. @@ -387,6 +399,7 @@ The following example converts the minimum or maximum value of other primitive numeric types to a value. :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/convert1.cs" id="Snippet20"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/convert1.fs" id="Snippet20"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.structure/vb/convert1.vb" id="Snippet20"::: In addition, the values , , and convert to , , and , respectively. @@ -404,11 +417,12 @@ In addition, , , and throw an for conversions to integers in a checked context, but these values overflow when converted to integers in an unchecked context. For conversions to , they always throw an . For conversions to , they convert to , , and , respectively. - Note that a loss of precision may result from converting a value to another numeric type. In the case of converting non-integral values, as the output from the example shows, the fractional component is lost when the value is either rounded (as in Visual Basic) or truncated (as in C#). For conversions to values, the value may not have a precise representation in the target data type. + Note that a loss of precision may result from converting a value to another numeric type. In the case of converting non-integral values, as the output from the example shows, the fractional component is lost when the value is either rounded (as in Visual Basic) or truncated (as in C# and F#). For conversions to values, the value may not have a precise representation in the target data type. - The following example converts a number of values to several other numeric types. The conversions occur in a checked context in Visual Basic (the default) and in C# (because of the [checked](/dotnet/csharp/language-reference/keywords/checked) keyword). The output from the example shows the result for conversions in both a checked an unchecked context. You can perform conversions in an unchecked context in Visual Basic by compiling with the `/removeintchecks+` compiler switch and in C# by commenting out the `checked` statement. + The following example converts a number of values to several other numeric types. The conversions occur in a checked context in Visual Basic (the default), in C# (because of the [checked](/dotnet/csharp/language-reference/keywords/checked) keyword), and in F# (because of the `open Checked` statement). The output from the example shows the result for conversions in both a checked an unchecked context. You can perform conversions in an unchecked context in Visual Basic by compiling with the `/removeintchecks+` compiler switch, in C# by commenting out the `checked` statement, and in F# by commenting out the `open Checked` statement. :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/convert2.cs" id="Snippet21"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/convert2.fs" id="Snippet21"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.structure/vb/convert2.vb" id="Snippet21"::: For more information on the conversion of numeric types, see [Type Conversion in the .NET Framework](/dotnet/standard/base-types/type-conversion) and [Type Conversion Tables](/dotnet/standard/base-types/conversion-tables). @@ -452,6 +466,7 @@ The problem of precision most frequently affects values that are converted to values. In the following example, two values produced by identical division operations are unequal, because one of the values is a single-precision floating point value that is converted to a . :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/precisionlist1.cs" interactive="try-dotnet" id="Snippet5"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/precisionlist1.fs" id="Snippet5"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.structure/vb/precisionlist1.vb" id="Snippet5"::: ]]> @@ -539,6 +554,7 @@ Values must be identical to be considered equal. Particularly when floating-point values depend on multiple mathematical operations, it is common for them to lose precision and for their values to be nearly identical except for their least significant digits. Because of this, the return value of the method may seem surprising at times. For example, multiplication by a particular value followed by division by the same value should produce the original value, but in the following example, the computed value turns out to be greater than the original value. Showing all significant digits of the two values by using the "R" [standard numeric format string](/dotnet/standard/base-types/standard-numeric-format-strings) indicates that the computed value differs from the original value in its least significant digits. For information about handling such comparisons, see the Remarks section of the method. :::code language="csharp" source="~/snippets/csharp/System/Single/CompareTo/compareto3.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/CompareTo/compareto3.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.compareto/vb/compareto3.vb" id="Snippet2"::: This method is implemented to support the interface. Note that, although a is not considered to be equal to another (even itself), the interface requires that `A.CompareTo(A)` return zero. @@ -553,6 +569,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Single/CPP/singlesample.cpp" id="Snippet16"::: :::code language="csharp" source="~/snippets/csharp/System/Single/CompareTo/singlesample.cs" id="Snippet16"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/CompareTo/singlesample.fs" id="Snippet16"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single/VB/singlesample.vb" id="Snippet16"::: ]]> @@ -632,6 +649,7 @@ Values must be identical to be considered equal. It is common for floating-point values to lose precision and to become nearly identical except for their least significant digits, especially when the values depend on multiple mathematical operations. Because of this, the return value of the method at times may seem surprising. For example, multiplication by any value followed by division by the same value should produce the original value. However, in the following example, the computed value turns out to be greater than the original value. Showing all significant digits of the two values by using the "R" [standard numeric format string](/dotnet/standard/base-types/standard-numeric-format-strings) indicates that the computed value differs from the original value in its least significant digits. For information about handling such comparisons, see the Remarks section of the method. :::code language="csharp" source="~/snippets/csharp/System/Single/CompareTo/compareto2.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/CompareTo/compareto2.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.compareto/vb/compareto2.vb" id="Snippet1"::: This method implements the interface and performs slightly better than the method because it does not have to convert the `value` parameter to an object. @@ -655,6 +673,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/T.CompareTo/CPP/cat.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Boolean/CompareTo/cat.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Boolean/CompareTo/cat.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/T.CompareTo/VB/cat.vb" id="Snippet1"::: ]]> @@ -708,11 +727,13 @@ The value of the property reflects the smallest positive value that is significant in numeric operations or comparisons when the value of the instance is zero. For example, the following code shows that zero and are considered to be unequal values, whereas zero and half the value of are considered to be equal. :::code language="csharp" source="~/snippets/csharp/System/Single/Epsilon/epsilon.cs" interactive="try-dotnet" id="Snippet5"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Epsilon/epsilon.fs" id="Snippet5"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single.Epsilon/vb/epsilon.vb" id="Snippet5"::: More precisely, the single-precision floating-point format consists of a sign, a 23-bit mantissa or significand, and an 8-bit exponent. As the following example shows, zero has an exponent of -126 and a mantissa of 0. has an exponent of -126 and a mantissa of 1. This means that is the smallest positive value that is greater than zero and represents the smallest possible value and the smallest possible increment for a whose exponent is -126. :::code language="csharp" source="~/snippets/csharp/System/Single/Epsilon/epsilon1.cs" interactive="try-dotnet" id="Snippet6"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Epsilon/epsilon1.fs" id="Snippet6"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single.Epsilon/vb/epsilon1.vb" id="Snippet6"::: However, the property is not a general measure of precision of the type; it applies only to instances that have a value of zero. @@ -796,11 +817,13 @@ The method should be used with caution, because two apparently equivalent values can be unequal due to the differing precision of the two values. The following example reports that the value .3333 and the returned by dividing 1 by 3 are unequal. :::code language="csharp" source="~/snippets/csharp/System/Single/Epsilon/SingleEquals_25051.cs" interactive="try-dotnet-method" id="Snippet3"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Epsilon/SingleEquals_25051.fs" id="Snippet3"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single.Epsilon/vb/SingleEquals_25051.vb" id="Snippet3"::: Rather than comparing for equality, one recommended technique involves defining an acceptable margin of difference between two values (such as .01% of one of the values). If the absolute value of the difference between the two values is less than or equal to that margin, the difference is likely to be due to differences in precision and, therefore, the values are likely to be equal. The following example uses this technique to compare .33333 and 1/3, the two values that the previous code example found to be unequal. :::code language="csharp" source="~/snippets/csharp/System/Single/Epsilon/SingleEquals_25051.cs" interactive="try-dotnet-method" id="Snippet4"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Epsilon/SingleEquals_25051.fs" id="Snippet4"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single.Epsilon/vb/SingleEquals_25051.vb" id="Snippet4"::: In this case, the values are equal. @@ -817,6 +840,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Single/CPP/singlesample.cpp" id="Snippet17"::: :::code language="csharp" source="~/snippets/csharp/System/Single/CompareTo/singlesample.cs" id="Snippet17"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/CompareTo/singlesample.fs" id="Snippet17"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single/VB/singlesample.vb" id="Snippet17"::: ]]> @@ -825,6 +849,7 @@ Compiler overload resolution may account for an apparent difference in the behavior of the two method overloads. If an implicit conversion between the argument and a is defined and the argument is not typed as an , compilers may perform an implicit conversion and call the method. Otherwise, they call the method, which always returns if its argument is not a value. The following example illustrates the difference in behavior between the two method overloads. In the case of all primitive numeric types except for in Visual Basic and except for and in C#, the first comparison returns because the compiler automatically performs a widening conversion and calls the method, whereas the second comparison returns because the compiler calls the method. :::code language="csharp" source="~/snippets/csharp/System/Single/Equals/equalsoverl.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Equals/equalsoverl.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.equals/vb/equalsoverl.vb" id="Snippet2"::: @@ -893,11 +918,13 @@ The method should be used with caution, because two apparently equivalent values can be unequal because of the differing precision of the two values. The following example reports that the value .3333 and the returned by dividing 1 by 3 are unequal. :::code language="csharp" source="~/snippets/csharp/System/Single/Epsilon/SingleEquals_25051.cs" interactive="try-dotnet-method" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Epsilon/SingleEquals_25051.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single.Epsilon/vb/SingleEquals_25051.vb" id="Snippet1"::: One comparison technique that avoids the problems associated with comparing for equality involves defining an acceptable margin of difference between two values (such as .01% of one of the values). If the absolute value of the difference between the two values is less than or equal to that margin, the difference is likely to be an outcome of differences in precision and, therefore, the values are likely to be equal. The following example uses this technique to compare .33333 and 1/3, which are the two values that the previous code example found to be unequal. :::code language="csharp" source="~/snippets/csharp/System/Single/Epsilon/SingleEquals_25051.cs" interactive="try-dotnet-method" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Epsilon/SingleEquals_25051.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single.Epsilon/vb/SingleEquals_25051.vb" id="Snippet2"::: In this case, the values are equal. @@ -908,6 +935,7 @@ A second technique that avoids the problems associated with comparing for equality involves comparing the difference between two floating-point numbers with some absolute value. If the difference is less than or equal to that absolute value, the numbers are equal. If it is greater, the numbers are not equal. One way to do this is to arbitrarily select an absolute value. However, this is problematic, because an acceptable margin of difference depends on the magnitude of the values. A second way takes advantage of a design feature of the floating-point format: The difference between the mantissa components in the integer representations of two floating-point values indicates the number of possible floating-point values that separates the two values. For example, the difference between 0.0 and is 1, because is the smallest representable value when working with a whose value is zero. The following example uses this technique to compare .33333 and 1/3, which are the two values that the previous code example with the method found to be unequal. Note that the example uses the and methods to convert a single-precision floating-point value to its integer representation. :::code language="csharp" source="~/snippets/csharp/System/Single/Equals/equalsabs1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Equals/equalsabs1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.equals/vb/equalsabs1.vb" id="Snippet1"::: The precision of floating-point numbers beyond the documented precision is specific to the implementation and version of the .NET Framework. Consequently, a comparison of two numbers might produce different results depending on the version of the .NET Framework, because the precision of the numbers' internal representation might change. @@ -918,6 +946,7 @@ Compiler overload resolution may account for an apparent difference in the behavior of the two method overloads. If an implicit conversion between the argument and a is defined and the argument is not typed as an , compilers may perform an implicit conversion and call the method. Otherwise, they call the method, which always returns if its argument is not a value. The following example illustrates the difference in behavior between the two method overloads. In the case of all primitive numeric types except for in Visual Basic and except for and in C#, the first comparison returns because the compiler automatically performs a widening conversion and calls the method, whereas the second comparison returns because the compiler calls the method. :::code language="csharp" source="~/snippets/csharp/System/Single/Equals/equalsoverl.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Equals/equalsoverl.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.equals/vb/equalsoverl.vb" id="Snippet2"::: @@ -1122,6 +1151,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Single/CPP/singlesample.cpp" id="Snippet11"::: :::code language="csharp" source="~/snippets/csharp/System/Single/CompareTo/singlesample.cs" interactive="try-dotnet-method" id="Snippet11"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/CompareTo/singlesample.fs" id="Snippet11"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single/VB/singlesample.vb" id="Snippet11"::: ]]> @@ -1205,6 +1235,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Single/CPP/singlesample.cpp" id="Snippet8"::: :::code language="csharp" source="~/snippets/csharp/System/Single/CompareTo/singlesample.cs" id="Snippet8"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/CompareTo/singlesample.fs" id="Snippet8"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single/VB/singlesample.vb" id="Snippet8"::: ]]> @@ -1318,6 +1349,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Single/CPP/singlesample.cpp" id="Snippet13"::: :::code language="csharp" source="~/snippets/csharp/System/Single/CompareTo/singlesample.cs" interactive="try-dotnet-method" id="Snippet13"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/CompareTo/singlesample.fs" id="Snippet13"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single/VB/singlesample.vb" id="Snippet13"::: ]]> @@ -1434,6 +1466,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Single/CPP/singlesample.cpp" id="Snippet12"::: :::code language="csharp" source="~/snippets/csharp/System/Single/CompareTo/singlesample.cs" interactive="try-dotnet-method" id="Snippet12"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/CompareTo/singlesample.fs" id="Snippet12"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single/VB/singlesample.vb" id="Snippet12"::: ]]> @@ -1532,6 +1565,7 @@ The result of an operation that exceeds is . In the following example, results from addition, multiplication, and exponentiation operations when the result exceeds . :::code language="csharp" source="~/snippets/csharp/System/Single/MaxValue/maxvalueex.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/MaxValue/maxvalueex.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.maxvalue/vb/maxvalueex.vb" id="Snippet1"::: @@ -1541,6 +1575,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Single/CPP/singlesample.cpp" id="Snippet4"::: :::code language="csharp" source="~/snippets/csharp/System/Single/CompareTo/singlesample.cs" id="Snippet4"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/CompareTo/singlesample.fs" id="Snippet4"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single/VB/singlesample.vb" id="Snippet4"::: ]]> @@ -1596,6 +1631,7 @@ The result of an operation that is less than is . In the following example, results from subtraction and multiplication operations when the result is less than . :::code language="csharp" source="~/snippets/csharp/System/Single/MinValue/minvalueex.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/MinValue/minvalueex.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.minvalue/vb/minvalueex.vb" id="Snippet1"::: @@ -1605,6 +1641,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Single/CPP/singlesample.cpp" id="Snippet5"::: :::code language="csharp" source="~/snippets/csharp/System/Single/CompareTo/singlesample.cs" id="Snippet5"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/CompareTo/singlesample.fs" id="Snippet5"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single/VB/singlesample.vb" id="Snippet5"::: ]]> @@ -1658,16 +1695,19 @@ A method or operator returns when the result of an operation is undefined. For example, the result of dividing zero by zero is , as the following example shows. (But note that dividing a non-zero number by zero returns either or , depending on the sign of the divisor.) :::code language="csharp" source="~/snippets/csharp/System/Single/NaN/nan1.cs" interactive="try-dotnet-method" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/NaN/nan1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.nan/vb/nan1.vb" id="Snippet1"::: In addition, a method call with a value or an operation on a value returns , as the following example shows. :::code language="csharp" source="~/snippets/csharp/System/Single/NaN/nan1.cs" interactive="try-dotnet-method" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/NaN/nan1.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.nan/vb/nan1.vb" id="Snippet2"::: Use the method to determine whether a value is not a number. In general, operators cannot be used to compare with other values, although comparison methods (such as and ) can. The following example illustrates the difference in behavior between comparison operators and methods. :::code language="csharp" source="~/snippets/csharp/System/Single/NaN/single.nan4.cs" interactive="try-dotnet" id="Snippet4"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/NaN/single.nan4.fs" id="Snippet4"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.nan/vb/single.nan4.vb" id="Snippet4"::: @@ -1677,6 +1717,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Single/CPP/singlesample.cpp" id="Snippet7"::: :::code language="csharp" source="~/snippets/csharp/System/Single/CompareTo/singlesample.cs" interactive="try-dotnet-method" id="Snippet7"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/CompareTo/singlesample.fs" id="Snippet7"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single/VB/singlesample.vb" id="Snippet7"::: ]]> @@ -1740,6 +1781,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Single/CPP/singlesample.cpp" id="Snippet9"::: :::code language="csharp" source="~/snippets/csharp/System/Single/CompareTo/singlesample.cs" interactive="try-dotnet-method" id="Snippet9"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/CompareTo/singlesample.fs" id="Snippet9"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single/VB/singlesample.vb" id="Snippet9"::: ]]> @@ -2186,6 +2228,7 @@ If a separator is encountered in the `s` parameter during a parse operation, and The following example uses the method to convert an array of strings to equivalent values. :::code language="csharp" source="~/snippets/csharp/System/Single/Parse/parse1.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Parse/parse1.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single.Parse/VB/parse1.vb" id="Snippet2"::: ]]> @@ -2326,6 +2369,7 @@ If a separator is encountered in the `s` parameter during a parse operation, and The following example uses the method to parse the string representations of values. The example uses formatting information for the en-US culture. :::code language="csharp" source="~/snippets/csharp/System/Single/Parse/parse2.cs" interactive="try-dotnet" id="Snippet3"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Parse/parse2.fs" id="Snippet3"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single.Parse/VB/parse2.vb" id="Snippet3"::: ]]> @@ -2637,6 +2681,7 @@ If a separator is encountered in the `s` parameter during a parse operation, and The following code example uses the method to parse the string representations of values. Each string in an array is parsed using the formatting conventions of the en-US, nl-NL, and a custom culture. The custom culture defines its group separator symbol as the underscore ("_") and its group size as two. :::code language="csharp" source="~/snippets/csharp/System/Single/Parse/parse3.cs" interactive="try-dotnet" id="Snippet4"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/Parse/parse3.fs" id="Snippet4"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single.Parse/VB/parse3.vb" id="Snippet4"::: ]]> @@ -2713,6 +2758,7 @@ If a separator is encountered in the `s` parameter during a parse operation, and :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Single/CPP/singlesample.cpp" id="Snippet10"::: :::code language="csharp" source="~/snippets/csharp/System/Single/CompareTo/singlesample.cs" interactive="try-dotnet-method" id="Snippet10"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/CompareTo/singlesample.fs" id="Snippet10"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single/VB/singlesample.vb" id="Snippet10"::: ]]> @@ -3740,12 +3786,14 @@ This member is an explicit interface member implementation. It can be used only The following example uses the default method to display the string representations of a number of values. :::code language="csharp" source="~/snippets/csharp/System/Single/ToString/ToString1.cs" interactive="try-dotnet-method" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/ToString/ToString1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single.ToString/VB/ToString1.vb" id="Snippet1"::: The following code example illustrates the use of the method along with the method. :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Single/CPP/singlesample.cpp" id="Snippet3"::: :::code language="csharp" source="~/snippets/csharp/System/Single/CompareTo/singlesample.cs" id="Snippet3"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/CompareTo/singlesample.fs" id="Snippet3"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single/VB/singlesample.vb" id="Snippet3"::: ]]> @@ -3855,6 +3903,7 @@ This member is an explicit interface member implementation. It can be used only The following example displays the string representation of two values using objects that represent several different cultures. :::code language="csharp" source="~/snippets/csharp/System/Single/ToString/ToString1.cs" interactive="try-dotnet-method" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/ToString/ToString1.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single.ToString/VB/ToString1.vb" id="Snippet2"::: ]]> @@ -3944,11 +3993,13 @@ This member is an explicit interface member implementation. It can be used only The following example defines a numeric value and formats it as a currency value by using the "C" standard numeric format string and as a numeric value to three decimal places by using the "N" standard numeric format string. The result strings are formatted by using the conventions of the en-US culture. For more information on numeric format strings, see [Standard Numeric Format Strings](/dotnet/standard/base-types/standard-numeric-format-strings) and [Custom Numeric Format Strings](/dotnet/standard/base-types/custom-numeric-format-strings). :::code language="csharp" source="~/snippets/csharp/System/Single/ToString/ToString7.cs" id="Snippet7"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/ToString/ToString7.fs" id="Snippet7"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single.ToString/VB/ToString7.vb" id="Snippet7"::: The following example displays several values using each of the supported standard numeric format specifiers together with two custom numeric format strings. One of those custom format strings illustrates how to pad a value with leading zeros. In converting the numeric values to strings, the example uses the formatting conventions of the en-US culture. :::code language="csharp" source="~/snippets/csharp/System/Single/ToString/ToString1.cs" id="Snippet3"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/ToString/ToString1.fs" id="Snippet3"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single.ToString/VB/ToString1.vb" id="Snippet3"::: ]]> @@ -4048,6 +4099,7 @@ This member is an explicit interface member implementation. It can be used only The following example displays a value using each of the supported standard numeric format specifiers for several different cultures. :::code language="csharp" source="~/snippets/csharp/System/Single/ToString/ToString1.cs" interactive="try-dotnet-method" id="Snippet4"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/ToString/ToString1.fs" id="Snippet4"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Single.ToString/VB/ToString1.vb" id="Snippet4"::: ]]> @@ -4255,6 +4307,7 @@ If a separator is encountered in the `s` parameter during a parse operation, and The following example uses the method to convert the string representations of numeric values to values. It assumes that en-US is the current culture. :::code language="csharp" source="~/snippets/csharp/System/Single/TryParse/tryparse1.cs" interactive="try-dotnet-method" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/TryParse/tryparse1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.tryparse/vb/tryparse1.vb" id="Snippet1"::: ]]> @@ -4436,6 +4489,7 @@ If `s` is out of range of the data type, the method throws The following example demonstrates the use of the method to parse the string representation of numbers that have a particular style and are formatted using the conventions of a particular culture. :::code language="csharp" source="~/snippets/csharp/System/Single/TryParse/tryparse1.cs" interactive="try-dotnet-method" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Single/TryParse/tryparse1.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.single.tryparse/vb/tryparse1.vb" id="Snippet2"::: ]]> From d3d7356a8afb1f2ae8a6223ac7cbffd99e5815e7 Mon Sep 17 00:00:00 2001 From: Albert Du <52804499+albert-du@users.noreply.github.com> Date: Sun, 20 Mar 2022 23:53:39 -0700 Subject: [PATCH 2/2] Add missing module declaration --- snippets/fsharp/System/Single/Equals/equalsabs1.fs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/snippets/fsharp/System/Single/Equals/equalsabs1.fs b/snippets/fsharp/System/Single/Equals/equalsabs1.fs index ac23fbafaff..be7777d1a44 100644 --- a/snippets/fsharp/System/Single/Equals/equalsabs1.fs +++ b/snippets/fsharp/System/Single/Equals/equalsabs1.fs @@ -1,4 +1,6 @@ -// +module equalsabs1 + +// open System let hasMinimalDifference (value1: float32) (value2: float32) units = @@ -22,4 +24,4 @@ let value2 = printfn $"{value1:R} = {value2:R}: {hasMinimalDifference value1 value2 1}" // The example displays the following output: // 1 = 1.0000001: True -// \ No newline at end of file +//