Skip to content

Commit 27bda10

Browse files
authored
UInt64 F# snippets (#8051)
1 parent 89bc21b commit 27bda10

File tree

21 files changed

+631
-0
lines changed

21 files changed

+631
-0
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="source.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//<snippet3>
2+
open System
3+
4+
type Temperature() =
5+
// The value holder
6+
let mutable m_value = 0uL
7+
8+
interface IComparable with
9+
/// IComparable.CompareTo implementation.
10+
member _.CompareTo(obj) =
11+
match obj with
12+
| :? Temperature as temp ->
13+
m_value.CompareTo temp.Value
14+
| _ ->
15+
invalidArg "obj" "object is not a Temperature"
16+
17+
member _.Value
18+
with get () =
19+
m_value
20+
and set (v) =
21+
m_value <- v
22+
//</snippet3>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module equals1
2+
3+
// <Snippet1>
4+
let values: obj[] =
5+
[| 10s; 20s; 10; 20
6+
10L; 20L; 10.; 20.; 10us
7+
20us; 10u; 20u
8+
10uL; 20uL |]
9+
let baseValue = 20uL
10+
let baseType = baseValue.GetType().Name
11+
12+
for value in values do
13+
printfn $"{baseValue} ({baseType}) = {value} ({value.GetType().Name}): {value.GetType().Name}"
14+
// The example displays the following output:
15+
// 20 (UInt64) = 10 (Int16): False
16+
// 20 (UInt64) = 20 (Int16): False
17+
// 20 (UInt64) = 10 (Int32): False
18+
// 20 (UInt64) = 20 (Int32): False
19+
// 20 (UInt64) = 10 (Int64): False
20+
// 20 (UInt64) = 20 (Int64): False
21+
// 20 (UInt64) = 10 (Double): False
22+
// 20 (UInt64) = 20 (Double): False
23+
// 20 (UInt64) = 10 (UInt16): False
24+
// 20 (UInt64) = 20 (UInt16): False
25+
// 20 (UInt64) = 10 (UInt32): False
26+
// 20 (UInt64) = 20 (UInt32): False
27+
// 20 (UInt64) = 10 (UInt64): False
28+
// 20 (UInt64) = 20 (UInt64): True
29+
// </Snippet1>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
module equalsoverl
2+
3+
// <Snippet2>
4+
let value = 112uL
5+
6+
let testObjectForEquality (obj: obj) =
7+
printfn $"{value} ({value.GetType().Name}) = {obj} ({obj.GetType().Name}): {value.Equals obj}\n"
8+
9+
let byte1= 112uy
10+
printfn $"value = byte1: {value.Equals byte1,16}"
11+
testObjectForEquality byte1
12+
13+
let short1 = 112s
14+
printfn $"value = short1: {value.Equals short1,17}"
15+
testObjectForEquality short1
16+
17+
let int1 = 112
18+
printfn $"value = int1: {value.Equals int1,19}"
19+
testObjectForEquality int1
20+
21+
let sbyte1 = 112y
22+
printfn $"value = sbyte1: {value.Equals sbyte1,17}"
23+
testObjectForEquality sbyte1
24+
25+
let ushort1 = 112us
26+
printfn $"value = ushort1: {value.Equals ushort1,16}"
27+
testObjectForEquality ushort1
28+
29+
let uint1 = 112u
30+
printfn $"value = uint1: {value.Equals uint1,18}"
31+
testObjectForEquality uint1
32+
33+
let dec1 = 112m
34+
printfn $"value = dec1: {value.Equals dec1,21}"
35+
testObjectForEquality dec1
36+
37+
let dbl1 = 112.
38+
printfn $"value = dbl1: {value.Equals dbl1,20}"
39+
testObjectForEquality dbl1
40+
41+
// The example displays the following output:
42+
// value = byte1: True
43+
// 112 (UInt64) = 112 (Byte): False
44+
//
45+
// value = short1: False
46+
// 112 (UInt64) = 112 (Int16): False
47+
//
48+
// value = int1: False
49+
// 112 (UInt64) = 112 (Int32): False
50+
//
51+
// value = sbyte1: False
52+
// 112 (UInt64) = 112 (SByte): False
53+
//
54+
// value = ushort1: True
55+
// 112 (UInt64) = 112 (UInt16): False
56+
//
57+
// value = uint1: True
58+
// 112 (UInt64) = 112 (UInt32): False
59+
//
60+
// value = dec1: False
61+
// 112 (UInt64) = 112 (Decimal): False
62+
//
63+
// value = dbl1: False
64+
// 112 (UInt64) = 112 (Double): False
65+
// </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="equals1.fs" />
9+
<Compile Include="equalsoverl.fs" />
10+
<Compile Include="uint64_equals.fs" />
11+
</ItemGroup>
12+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module uint64_equals
2+
3+
// <Snippet1>
4+
let value1 = 50uL
5+
let value2 = 50uL
6+
7+
// Display the values.
8+
printfn $"value1: Type: {value1.GetType().Name} Value: {value1}"
9+
printfn $"value2: Type: {value2.GetType().Name} Value: {value2}"
10+
11+
// Compare the two values.
12+
printfn $"value1 and value2 are equal: {value1.Equals value2}"
13+
// The example displays the following output:
14+
// value1: Type: UInt64 Value: 50
15+
// value2: Type: UInt64 Value: 50
16+
// value1 and value2 are equal: True
17+
// </Snippet1>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// <Snippet1>
2+
open System
3+
4+
let decimalValue = -1.5
5+
6+
// Discard fractional portion of Double value
7+
let decimalInteger = floor decimalValue
8+
9+
if decimalInteger <= float UInt64.MaxValue && decimalInteger >= float UInt64.MinValue then
10+
let integerValue = uint64 decimalValue
11+
printfn $"Converted {decimalValue} to {integerValue}."
12+
else
13+
let rangeLimit, relationship =
14+
if decimalInteger > float UInt64.MaxValue then
15+
UInt64.MaxValue, "greater"
16+
else
17+
UInt64.MinValue, "less"
18+
19+
printfn $"Conversion failure: {decimalInteger} is {relationship} than {rangeLimit}."
20+
// </Snippet1>
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="MaxValue1.fs" />
9+
</ItemGroup>
10+
</Project>
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="parse1.fs" />
9+
<Compile Include="parseex2.fs" />
10+
<Compile Include="parseex4.fs" />
11+
</ItemGroup>
12+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module parse1
2+
3+
open System
4+
5+
// <Snippet1>
6+
let values =
7+
[| "+13230"; "-0"; "1,390,146"; "$190,235,421,127"
8+
"0xFA1B"; "163042"; "-10"; "14065839182"
9+
"16e07"; "134985.0"; "-12034" |]
10+
for value in values do
11+
try
12+
let number = UInt64.Parse value
13+
printfn $"{value} --> {number}"
14+
with
15+
| :? FormatException ->
16+
printfn $"{value}: Bad Format"
17+
| :? OverflowException ->
18+
printfn $"{value}: Overflow"
19+
// The example displays the following output:
20+
// +13230 --> 13230
21+
// -0 --> 0
22+
// 1,390,146: Bad Format
23+
// $190,235,421,127: Bad Format
24+
// 0xFA1B: Bad Format
25+
// 163042 --> 163042
26+
// -10: Overflow
27+
// 14065839182 --> 14065839182
28+
// 16e07: Bad Format
29+
// 134985.0: Bad Format
30+
// -12034: Overflow
31+
// </Snippet1>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
module parseex2
2+
3+
// <Snippet2>
4+
open System
5+
open System.Globalization
6+
7+
let values =
8+
[| " 214309 "; "1,064,181"; "(0)"; "10241+"; " + 21499 "
9+
" +21499 "; "122153.00"; "1e03ff"; "91300.0e-2" |]
10+
let whitespace = NumberStyles.AllowLeadingWhite ||| NumberStyles.AllowTrailingWhite
11+
let styles =
12+
[| NumberStyles.None; whitespace
13+
NumberStyles.AllowLeadingSign ||| NumberStyles.AllowTrailingSign ||| whitespace
14+
NumberStyles.AllowThousands ||| NumberStyles.AllowCurrencySymbol
15+
NumberStyles.AllowExponent ||| NumberStyles.AllowDecimalPoint |]
16+
17+
// Attempt to convert each number using each style combination.
18+
for value in values do
19+
printfn $"Attempting to convert '{value}':"
20+
for style in styles do
21+
try
22+
let number = UInt64.Parse(value, style)
23+
printfn $" {style}: {number}"
24+
with
25+
| :? FormatException ->
26+
printfn $" {style}: Bad Format"
27+
| :? OverflowException ->
28+
printfn $" {value}: Overflow"
29+
printfn ""
30+
// The example displays the following output:
31+
// Attempting to convert ' 214309 ':
32+
// None: Bad Format
33+
// AllowLeadingWhite, AllowTrailingWhite: 214309
34+
// Integer, AllowTrailingSign: 214309
35+
// AllowThousands, AllowCurrencySymbol: Bad Format
36+
// AllowDecimalPoint, AllowExponent: Bad Format
37+
//
38+
// Attempting to convert '1,064,181':
39+
// None: Bad Format
40+
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
41+
// Integer, AllowTrailingSign: Bad Format
42+
// AllowThousands, AllowCurrencySymbol: 1064181
43+
// AllowDecimalPoint, AllowExponent: Bad Format
44+
//
45+
// Attempting to convert '(0)':
46+
// None: Bad Format
47+
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
48+
// Integer, AllowTrailingSign: Bad Format
49+
// AllowThousands, AllowCurrencySymbol: Bad Format
50+
// AllowDecimalPoint, AllowExponent: Bad Format
51+
//
52+
// Attempting to convert '10241+':
53+
// None: Bad Format
54+
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
55+
// Integer, AllowTrailingSign: 10241
56+
// AllowThousands, AllowCurrencySymbol: Bad Format
57+
// AllowDecimalPoint, AllowExponent: Bad Format
58+
//
59+
// Attempting to convert ' + 21499 ':
60+
// None: Bad Format
61+
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
62+
// Integer, AllowTrailingSign: Bad Format
63+
// AllowThousands, AllowCurrencySymbol: Bad Format
64+
// AllowDecimalPoint, AllowExponent: Bad Format
65+
//
66+
// Attempting to convert ' +21499 ':
67+
// None: Bad Format
68+
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
69+
// Integer, AllowTrailingSign: 21499
70+
// AllowThousands, AllowCurrencySymbol: Bad Format
71+
// AllowDecimalPoint, AllowExponent: Bad Format
72+
//
73+
// Attempting to convert '122153.00':
74+
// None: Bad Format
75+
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
76+
// Integer, AllowTrailingSign: Bad Format
77+
// AllowThousands, AllowCurrencySymbol: Bad Format
78+
// AllowDecimalPoint, AllowExponent: 122153
79+
//
80+
// Attempting to convert '1e03ff':
81+
// None: Bad Format
82+
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
83+
// Integer, AllowTrailingSign: Bad Format
84+
// AllowThousands, AllowCurrencySymbol: Bad Format
85+
// AllowDecimalPoint, AllowExponent: Bad Format
86+
//
87+
// Attempting to convert '91300.0e-2':
88+
// None: Bad Format
89+
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
90+
// Integer, AllowTrailingSign: Bad Format
91+
// AllowThousands, AllowCurrencySymbol: Bad Format
92+
// AllowDecimalPoint, AllowExponent: 913
93+
// </Snippet2>

0 commit comments

Comments
 (0)