1+ module singlesample
2+
3+ open System
4+
5+ [<EntryPoint>]
6+ let main _ =
7+ //<snippet1>
8+ let s = 4.55 f
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 = 0 f
47+
48+ // This condition will return false.
49+ if 0 f / 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( 0 f / 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 = 500 f
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
0 commit comments