-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSection_12.fs
143 lines (115 loc) · 4.79 KB
/
Section_12.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
namespace FPinFSharp.Chapter_02
// 2.12 Summary of the basic types
module Section_12 =
// Helpers.
let formatNumeric (values: 'T * 'T) : string * string =
System.String.Format("{0}", fst values), System.String.Format("{0}", snd values)
let formatHexadecimal (values: 'T * 'T) : string * string =
if fst values < Unchecked.defaultof<'T>
then System.String.Format("-0x{0:X2}", fst values), System.String.Format("0x{0:X2}", snd values)
else System.String.Format("0x{0:X2}", fst values), System.String.Format("0x{0:X2}", snd values)
// Char
let convertIntToChar (x: int) : char =
System.Convert.ToChar x
// Byte
let getMinMaxByte (isHex: bool) : byte * byte =
match isHex with
| true -> (0x0uy, 0xFFuy)
| false -> (0uy, 255uy)
let getMinMaxByteInString (isHex: bool) : string * string =
match isHex with
| true -> getMinMaxByte true |> formatHexadecimal
| false -> getMinMaxByte false |> formatNumeric
// SByte
let getMinMaxSByte (isHex: bool) : sbyte * sbyte =
match isHex with
| true -> (0x80y, 0x7Fy)
| false -> (-128y, 127y)
let getMinMaxSByteInString (isHex: bool) : string * string =
match isHex with
| true -> getMinMaxSByte true |> formatHexadecimal
| false -> getMinMaxSByte false |> formatNumeric
// Int16
let getMinMaxInt16 (isHex: bool) : int16 * int16 =
match isHex with
| true -> (-0x8000s, 0x7FFFs)
| false -> (-32768s, 32767s)
let getMinMaxInt16InString (isHex: bool) : string * string =
match isHex with
| true -> getMinMaxInt16 true |> formatHexadecimal
| false -> getMinMaxInt16 false |> formatNumeric
// UInt16
let getMinMaxUInt16 (isHex: bool) : uint16 * uint16 =
match isHex with
| true -> (0x0us, 0xFFFFus)
| false -> (0us, 65535us)
let getMinMaxUInt16InString (isHex: bool) : string * string =
match isHex with
| true -> getMinMaxUInt16 true |> formatHexadecimal
| false -> getMinMaxUInt16 false |> formatNumeric
// Int32
let getInt32 (isHex: bool) : int32 * int32 =
match isHex with
| true -> (-0x80000000, 0x7FFFFFFF)
| false -> (-2147483648, 2147483647)
let getInt32InString (isHex: bool) : string * string =
match isHex with
| true -> getInt32 true |> formatHexadecimal
| false -> getInt32 false |> formatNumeric
// UInt32
let getUInt32 (isHex: bool) : uint32 * uint32 =
match isHex with
| true -> (0u, 0xFFFFFFFFu)
| false -> (0u, 4294967295u)
let getUInt32InString (isHex: bool) : string * string =
match isHex with
| true -> getUInt32 true |> formatHexadecimal
| false -> getUInt32 false |> formatNumeric
// Int64
let getInt64 (isHex: bool) : int64 * int64 =
match isHex with
| true -> (-0x8000000000000000L, 0x7FFFFFFFFFFFFFFFL)
| false -> (-9223372036854775808L, 9223372036854775807L)
let getInt64InString (isHex: bool) : string * string =
match isHex with
| true -> getInt64 true |> formatHexadecimal
| false -> getInt64 false |> formatNumeric
// UInt64
let getUInt64 (isHex: bool) : uint64 * uint64 =
match isHex with
| true -> (0UL, 0xFFFFFFFFFFFFFFFFUL)
| false -> (0UL, 18446744073709551615UL)
let getUInt64InString (isHex: bool) : string * string =
match isHex with
| true -> getUInt64 true |> formatHexadecimal
| false -> getUInt64 false |> formatNumeric
// nativeint
let getNativeInt (isHex: bool) : nativeint * nativeint =
match isHex with
| true -> (-0x80000000n, 0x7FFFFFFFn)
| false -> (-2147483648n, 2147483647n)
let getNativeIntInString (isHex: bool) : string * string =
match isHex with
| true -> getNativeInt true |> formatHexadecimal
| false -> getNativeInt false |> formatNumeric
// unativeint
let getUNativeInt (isHex: bool) : unativeint * unativeint =
match isHex with
| true -> (0un, 0xFFFFFFFFun)
| false -> (0un, 4294967295un)
let getUNativeIntInString (isHex: bool) : string * string =
match isHex with
| true -> getUNativeInt true |> formatHexadecimal
| false -> getUNativeInt false |> formatNumeric
// Single
let getMinMaxSingle () : single * single =
(-3.40282346639e+38f, 3.40282346639e+38f)
// Double
let getMinMaxDouble () : double * double =
(-1.7976931348623157e+308, 1.7976931348623157e+308)
// Decimal
let getMinMaxDecimal () : decimal * decimal =
(System.Decimal.MinValue, System.Decimal.MaxValue)
// BigInt
let getBigInt () : bigint =
18446744073709551615I