Skip to content

Commit 27c1bd2

Browse files
authored
System.Single F# snippets (#7850)
* Single F# snippets * Add missing module declaration
1 parent f9f76d9 commit 27c1bd2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1523
-2
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="precisionlist1.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// <Snippet5>
2+
let value1 = 1. / 3.
3+
let sValue2 = 1f / 3f
4+
let value2 = double sValue2
5+
printfn $"{value1:R} = {value2:R}: {value1.Equals value2}"
6+
// The example displays the following output:
7+
// 0.33333333333333331 = 0.3333333432674408: False
8+
// </Snippet5>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module compareto2
2+
3+
// <Snippet1>
4+
let value1 = 16.5457f
5+
let operand = 3.8899982f
6+
let value2 = value1 * operand / operand
7+
printfn $"Comparing {value1} and {value2}: {value1.CompareTo value2}\n"
8+
printfn $"Comparing {value1:R} and {value2:R}: {value1.CompareTo value2}"
9+
// The example displays the following output:
10+
// Comparing 16.5457 and 16.5457: -1
11+
//
12+
// Comparing 16.5457 and 16.545702: -1
13+
// </Snippet1>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module compareto3
2+
3+
// <Snippet2>
4+
let value1 = 16.5457f
5+
let operand = 3.8899982f
6+
let value2 = box (value1 * operand / operand)
7+
printfn $"Comparing {value1} and {value2}: {value1.CompareTo value2}\n"
8+
printfn $"Comparing {value1:R} and {value2:R}: {value1.CompareTo value2}"
9+
// The example displays the following output:
10+
// Comparing 16.5457 and 16.5457: -1
11+
//
12+
// Comparing 16.5457 and 16.545702: -1
13+
// </Snippet2>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="compareto3.fs" />
9+
<Compile Include="compareto2.fs" />
10+
<Compile Include="singlesample.fs" />
11+
</ItemGroup>
12+
</Project>
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
module singlesample
2+
3+
open System
4+
5+
[<EntryPoint>]
6+
let main _ =
7+
//<snippet1>
8+
let s = 4.55f
9+
//</snippet1>
10+
11+
//<snippet2>
12+
printfn $"A Single is of type {s.GetType()}."
13+
//</snippet2>
14+
15+
//<snippet3>
16+
let mutable finished = false
17+
while not finished do
18+
printf "Enter a real number: "
19+
let inp = stdin.ReadLine()
20+
try
21+
let s = Single.Parse inp
22+
printfn $"You entered {s}."
23+
finished <- true
24+
with
25+
| :? FormatException ->
26+
printfn "You did not enter a number."
27+
| e ->
28+
printfn "An exception occurred while parsing your response: {e}"
29+
//</snippet3>
30+
31+
//<snippet4>
32+
if s > Single.MaxValue then
33+
printfn "Your number is larger than a Single."
34+
//</snippet4>
35+
36+
//<snippet5>
37+
if s < Single.MinValue then
38+
printfn "Your number is smaller than a Single."
39+
//</snippet5>
40+
41+
//<snippet6>
42+
printfn $"Epsilon, or the permittivity of a vacuum, has value {Single.Epsilon}"
43+
//</snippet6>
44+
45+
//<snippet7>
46+
let zero = 0f
47+
48+
// This condition will return false.
49+
if 0f / zero = Single.NaN then
50+
printfn "0 / 0 can be tested with Single.NaN."
51+
else
52+
printfn "0 / 0 cannot be tested with Single.NaN use Single.IsNan() instead."
53+
//</snippet7>
54+
55+
//<snippet8>
56+
// This will return true.
57+
if Single.IsNaN(0f / zero) then
58+
printfn "Single.IsNan() can determine whether a value is not-a-number."
59+
//</snippet8>
60+
61+
//<snippet9>
62+
// This will equal Infinity.
63+
printfn $"10.0 minus NegativeInfinity equals {10f - Single.NegativeInfinity}."
64+
//</snippet9>
65+
66+
//<snippet10>
67+
// This will equal Infinity.
68+
printfn $"PositiveInfinity plus 10.0 equals {Single.PositiveInfinity + 10f}."
69+
//</snippet10>
70+
71+
//<snippet11>
72+
// This will return "true".
73+
printfn $"IsInfinity(3.0F / 0) == %b{Single.IsInfinity(3f / 0f)}."
74+
//</snippet11>
75+
76+
//<snippet12>
77+
// This will return true.
78+
printfn $"IsPositiveInfinity(4.0F / 0) == {Single.IsPositiveInfinity(4f / 0f)}."
79+
//</snippet12>
80+
81+
//<snippet13>
82+
// This will return true.
83+
printfn $"IsNegativeInfinity(-5.0F / 0) == {Single.IsNegativeInfinity(-5f / 0f)}."
84+
//</snippet13>
85+
86+
//<snippet14>
87+
let a = 500f
88+
//</snippet14>
89+
90+
//<snippet15>
91+
// The variables point to the same objects.
92+
let mutable obj1: obj = a
93+
let obj2: obj = obj1
94+
95+
if Single.ReferenceEquals(obj1, obj2) then
96+
printfn "The variables point to the same Single object."
97+
else
98+
printfn "The variables point to different Single objects."
99+
//</snippet15>
100+
101+
//<snippet16>
102+
let obj1 = single 450
103+
104+
if a.CompareTo obj1 < 0 then
105+
printfn $"{a} is less than {obj1}."
106+
107+
if a.CompareTo obj1 > 0 then
108+
printfn $"{a} is greater than {obj1}."
109+
110+
if a.CompareTo obj1 = 0 then
111+
printfn $"{a} equals {obj1}."
112+
//</snippet16>
113+
114+
//<snippet17>
115+
let obj1 = single 500
116+
if a.Equals obj1 then
117+
printfn "The value type and reference type values are equal."
118+
//</snippet17>
119+
0
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module SingleEquals_25021
2+
3+
open System
4+
5+
let compareUsingEquals () =
6+
// <Snippet1>
7+
// Initialize two floats with apparently identical values
8+
let float1 = 0.33333f
9+
let float2 = 1f / 3f
10+
// Compare them for equality
11+
printfn $"{float1.Equals float2}" // displays false
12+
// </Snippet1>
13+
14+
let compareApproximateValues () =
15+
// <Snippet2>
16+
// Initialize two floats with apparently identical values
17+
let float1 = 0.33333f
18+
let float2 = 1f / 3f
19+
// Define the tolerance for variation in their values
20+
let difference = abs (float1 * 0.0001f)
21+
22+
// Compare the values
23+
// The output to the console indicates that the two values are equal
24+
if abs (float1 - float2) <= difference then
25+
printfn "float1 and float2 are equal."
26+
else
27+
printfn "float1 and float2 are unequal."
28+
// </Snippet2>
29+
30+
let compareObjectsUsingEquals () =
31+
// <Snippet3>
32+
// Initialize two floats with apparently identical values
33+
let float1 = 0.33333f
34+
let float2 = box (1f / 3f)
35+
// Compare them for equality
36+
printfn $"{float1.Equals float2}" // displays false
37+
// </Snippet3>
38+
39+
let compareApproximateObjectValues () =
40+
// <Snippet4>
41+
// Initialize two floats with apparently identical values
42+
let float1 = 0.33333f
43+
let float2 = box (1f / 3f)
44+
// Define the tolerance for variation in their values
45+
let difference = abs (float1 * 0.0001f)
46+
47+
// Compare the values
48+
// The output to the console indicates that the two values are equal
49+
if abs (float1 - (float2 :?> float32)) <= difference then
50+
printfn "float1 and float2 are equal."
51+
else
52+
printfn "float1 and float2 are unequal."
53+
// </Snippet4>
54+
55+
compareUsingEquals ()
56+
printfn ""
57+
compareApproximateValues ()
58+
printfn ""
59+
compareObjectsUsingEquals ()
60+
printfn ""
61+
compareApproximateObjectValues ()
62+
printfn ""
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module epsilon
2+
3+
// <Snippet5>
4+
open System
5+
6+
let values = [ 0f; Single.Epsilon; Single.Epsilon * 0.5f ]
7+
8+
for i = 0 to values.Length - 2 do
9+
for i2 = i + 1 to values.Length - 1 do
10+
printfn $"{values[i]:r} = {values[i2]:r}: {values[i].Equals(values[i2])}"
11+
printfn ""
12+
// The example displays the following output:
13+
// 0 = 1.401298E-45: False
14+
// 0 = 0: True
15+
//
16+
// 1.401298E-45 = 0: False
17+
// </Snippet5>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module epsilon1
2+
3+
// <Snippet6>
4+
open System
5+
6+
let getComponentParts (value: float32) =
7+
let result = $"{value:R}: "
8+
let indent = result.Length
9+
10+
// Convert the single to a 4-byte array.
11+
let bytes = BitConverter.GetBytes value
12+
let formattedSingle = BitConverter.ToInt32(bytes, 0)
13+
14+
// Get the sign bit (byte 3, bit 7).
15+
let result = result + $"""Sign: {if formattedSingle >>> 31 <> 0 then "1 (-)" else "0 (+)"}\n"""
16+
17+
// Get the exponent (byte 2 bit 7 to byte 3, bits 6)
18+
let exponent = (formattedSingle >>> 23) &&& 0x000000FF
19+
let adjustment = if exponent <> 0 then 127 else 126
20+
let result = result + $"{String(' ', indent)}Exponent: 0x{1:X4} ({exponent - adjustment})\n"
21+
22+
// Get the significand (bits 0-22)
23+
let significand =
24+
if exponent <> 0 then
25+
(formattedSingle &&& 0x007FFFFF) ||| 0x800000
26+
else
27+
formattedSingle &&& 0x007FFFFF
28+
29+
result + $"{String(' ', indent)}Mantissa: 0x{significand:X13}\n"
30+
31+
32+
let values = [ 0f; Single.Epsilon ]
33+
for value in values do
34+
printfn $"{getComponentParts value}\n"
35+
// // The example displays the following output:
36+
// 0: Sign: 0 (+)
37+
// Exponent: 0xFFFFFF82 (-126)
38+
// Mantissa: 0x0000000000000
39+
//
40+
//
41+
// 1.401298E-45: Sign: 0 (+)
42+
// Exponent: 0xFFFFFF82 (-126)
43+
// Mantissa: 0x0000000000001
44+
// </Snippet6>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="epsilon.fs" />
9+
<Compile Include="epsilon1.fs" />
10+
<Compile Include="SingleEquals_25051.fs" />
11+
</ItemGroup>
12+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module equalsabs1
2+
3+
// <Snippet1>
4+
open System
5+
6+
let hasMinimalDifference (value1: float32) (value2: float32) units =
7+
let bytes = BitConverter.GetBytes value1
8+
let iValue1 = BitConverter.ToInt32(bytes, 0)
9+
let bytes = BitConverter.GetBytes(value2)
10+
let iValue2 = BitConverter.ToInt32(bytes, 0)
11+
12+
// If the signs are different, return false except for +0 and -0.
13+
if (iValue1 >>> 31) <> (iValue2 >>> 31) then
14+
value1 = value2
15+
else
16+
let diff = abs (iValue1 - iValue2)
17+
diff <= units
18+
19+
let value1 = 0.1f * 10f
20+
let value2 =
21+
List.replicate 10 0.1f
22+
|> List.sum
23+
24+
printfn $"{value1:R} = {value2:R}: {hasMinimalDifference value1 value2 1}"
25+
// The example displays the following output:
26+
// 1 = 1.0000001: True
27+
// </Snippet1>

0 commit comments

Comments
 (0)