Skip to content

Commit 267a9e3

Browse files
authored
Math F# snippets (#7789)
1 parent c619764 commit 267a9e3

Some content is hidden

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

64 files changed

+1872
-2
lines changed

snippets/fsharp/System/Decimal/Round/fs.fsproj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44
<TargetFramework>net6.0</TargetFramework>
55
</PropertyGroup>
66
<ItemGroup>
7-
<Compile Include="Round1.fs" />
8-
<Compile Include="Round12.fs" />
7+
<Compile Include="mean1.fs" />
98
<Compile Include="midpoint1.fs" />
9+
<Compile Include="midpoint2.fs" />
1010
<Compile Include="mpr.fs" />
11+
<Compile Include="precision1.fs" />
12+
<Compile Include="precision2.fs" />
13+
<Compile Include="Round1.fs" />
14+
<Compile Include="Round12.fs" />
15+
<Compile Include="Round12.fs" />
16+
<Compile Include="single1.fs" />
17+
<Compile Include="source.fs" />
1118
</ItemGroup>
1219
</Project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module mean1
2+
3+
// <Snippet2>
4+
open System
5+
6+
let values = [| 1.15m; 1.25m; 1.35m; 1.45m; 1.55m; 1.65m |]
7+
let mutable sum = 0m
8+
9+
// Calculate true mean.
10+
for value in values do
11+
sum <- sum + value
12+
13+
printfn $"True mean: {sum / decimal values.Length:N2}"
14+
15+
// Calculate mean with rounding away from zero.
16+
sum <- 0m
17+
for value in values do
18+
sum <- sum + Math.Round(value, 1, MidpointRounding.AwayFromZero)
19+
20+
printfn $"AwayFromZero: {sum / decimal values.Length:N2}"
21+
22+
// Calculate mean with rounding to nearest.
23+
sum <- 0m
24+
for value in values do
25+
sum <- sum + Math.Round(value, 1, MidpointRounding.ToEven)
26+
27+
printfn $"ToEven: {sum / decimal values.Length:N2}"
28+
29+
// The example displays the following output:
30+
// True mean: 1.40
31+
// AwayFromZero: 1.45
32+
// ToEven: 1.40
33+
// </Snippet2>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module midpoint2
2+
3+
// <Snippet6>
4+
open System
5+
6+
let values =
7+
[| 12.; 12.1; 12.2; 12.3; 12.4; 12.5
8+
12.6; 12.7; 12.8; 12.9; 13. |]
9+
10+
printfn "%-10s %-10s %-10s %-15s %-15s" "Value" "Default" "ToEven" "AwayFromZero" "ToZero"
11+
12+
for value in values do
13+
$"{value,-10:R} {Math.Round(value),-10} " +
14+
$"{Math.Round(value, MidpointRounding.ToEven),-10} " +
15+
$"{Math.Round(value, MidpointRounding.AwayFromZero),-15} " +
16+
$"{Math.Round(value, MidpointRounding.ToZero),-15}"
17+
|> printfn "%s"
18+
19+
// The example displays the following output:
20+
// Value Default ToEven AwayFromZero ToZero
21+
// 12 12 12 12 12
22+
// 12.1 12 12 12 12
23+
// 12.2 12 12 12 12
24+
// 12.3 12 12 12 12
25+
// 12.4 12 12 12 12
26+
// 12.5 12 12 13 12
27+
// 12.6 13 13 13 12
28+
// 12.7 13 13 13 12
29+
// 12.8 13 13 13 12
30+
// 12.9 13 13 13 12
31+
// 13 13 13 13 13
32+
// </Snippet6>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module precision1
2+
3+
// <Snippet7>
4+
open System
5+
6+
let roundValueAndAdd (value: double) =
7+
printfn $"{value,5:N1} {value,20:R} {Math.Round(value, MidpointRounding.ToEven),12} {Math.Round(value, MidpointRounding.AwayFromZero),15}"
8+
value + 0.1
9+
10+
printfn "%5s %20s %12s %15s\n" "Value" "Full Precision" "ToEven" "AwayFromZero"
11+
let mutable value = 11.1
12+
for _ = 0 to 5 do
13+
value <- roundValueAndAdd value
14+
15+
printfn ""
16+
17+
value <- 11.5
18+
roundValueAndAdd value
19+
|> ignore
20+
21+
// The example displays the following output:
22+
// Value Full Precision ToEven AwayFromZero
23+
//
24+
// 11.1 11.1 11 11
25+
// 11.2 11.2 11 11
26+
// 11.3 11.299999999999999 11 11
27+
// 11.4 11.399999999999999 11 11
28+
// 11.5 11.499999999999998 11 11
29+
// 11.6 11.599999999999998 12 12
30+
//
31+
// 11.5 11.5 12 12
32+
// </Snippet7>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
module precision2
2+
3+
// <Snippet8>
4+
open System
5+
6+
let roundApproximate dbl digits margin mode =
7+
let fraction = dbl * Math.Pow(10, digits)
8+
let value = Math.Truncate fraction
9+
let fraction = fraction - value
10+
if fraction = 0 then
11+
dbl
12+
else
13+
let tolerance = margin * dbl
14+
// Determine whether this is a midpoint value.
15+
if (fraction >= 0.5 - tolerance) && (fraction <= 0.5 + tolerance) then
16+
if mode = MidpointRounding.AwayFromZero then
17+
(value + 1.) / Math.Pow(10, digits)
18+
elif value % 2. <> 0 then
19+
(value + 1.) / Math.Pow(10, digits)
20+
else
21+
value / Math.Pow(10, digits)
22+
// Any remaining fractional value greater than .5 is not a midpoint value.
23+
elif fraction > 0.5 then
24+
(value + 1.) / Math.Pow(10, digits)
25+
else
26+
value / Math.Pow(10, digits)
27+
28+
29+
let roundValueAndAdd value =
30+
let tolerance = 8e-14
31+
let round = roundApproximate value 0 tolerance
32+
33+
printfn $"{value,5:N1} {value,20:R} {round MidpointRounding.ToEven,12} {round MidpointRounding.AwayFromZero,15}"
34+
value + 0.1
35+
36+
printfn "%5s %20s %12s %15s\n" "Value" "Full Precision" "ToEven" "AwayFromZero"
37+
let mutable value = 11.1
38+
for _ = 0 to 5 do
39+
value <- roundValueAndAdd value
40+
41+
printfn ""
42+
43+
value <- 11.5
44+
roundValueAndAdd value
45+
|> ignore
46+
47+
// The example displays the following output:
48+
// Value Full Precision ToEven AwayFromZero
49+
//
50+
// 11.1 11.1 11 11
51+
// 11.2 11.2 11 11
52+
// 11.3 11.299999999999999 11 11
53+
// 11.4 11.399999999999999 11 11
54+
// 11.5 11.499999999999998 12 12
55+
// 11.6 11.599999999999998 12 12
56+
//
57+
// 11.5 11.5 12 12
58+
// </Snippet8>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module single1
2+
3+
// <Snippet1>
4+
// In F#, 'float', 'float64', and 'double' are aliases for System.Double...
5+
// 'float32' and 'single' are aliases for System.Single
6+
open System
7+
8+
let value = 16.325f
9+
printfn $"Widening Conversion of {value:R} (type {value.GetType().Name}) to {double value:R} (type {(double value).GetType().Name}): "
10+
printfn $"{Math.Round(decimal value, 2)}"
11+
printfn $"{Math.Round(decimal value, 2, MidpointRounding.AwayFromZero)}"
12+
printfn ""
13+
14+
let decValue = decimal value
15+
printfn $"Cast of {value:R} (type {value.GetType().Name}) to {decValue} (type {decValue.GetType().Name}): "
16+
printfn $"{Math.Round(decValue, 2)}"
17+
printfn $"{Math.Round(decValue, 2, MidpointRounding.AwayFromZero)}"
18+
19+
// The example displays the following output:
20+
// Widening Conversion of 16.325 (type Single) to 16.325000762939453 (type Double):
21+
// 16.33
22+
// 16.33
23+
//
24+
// Cast of 16.325 (type Single) to 16.325 (type Decimal):
25+
// 16.32
26+
// 16.33
27+
// </Snippet1>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module source
2+
3+
// <Snippet3>
4+
open System
5+
6+
printfn
7+
$"""{Math.Round(3.44m, 1)}
8+
{Math.Round(3.45m, 1)}
9+
{Math.Round(3.46m, 1)}
10+
11+
{Math.Round(4.34m, 1)}
12+
{Math.Round(4.35m, 1)}
13+
{Math.Round(4.36m, 1)}"""
14+
15+
// The example displays the following output:
16+
// 3.4
17+
// 3.4
18+
// 3.5
19+
//
20+
// 4.3
21+
// 4.4
22+
// 4.4
23+
// </Snippet3>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Abs1
2+
3+
// <Snippet1>
4+
open System
5+
6+
let decimals =
7+
[ Decimal.MaxValue; 12.45M; 0M
8+
-19.69M; Decimal.MinValue ]
9+
10+
for value in decimals do
11+
// The 'abs' function may be used instead.
12+
printfn $"Abs({value}) = {Math.Abs value}"
13+
14+
// The example displays the following output:
15+
// Abs(79228162514264337593543950335) = 79228162514264337593543950335
16+
// Abs(12.45) = 12.45
17+
// Abs(0) = 0
18+
// Abs(-19.69) = 19.69
19+
// Abs(-79228162514264337593543950335) = 79228162514264337593543950335
20+
// </Snippet1>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module abs2
2+
3+
// <Snippet2>
4+
open System
5+
6+
let doubles =
7+
[ Double.MaxValue; 16.354e-17; 15.098123; 0
8+
-19.069713; -15.058e18; Double.MinValue ]
9+
10+
for value in doubles do
11+
// The 'abs' function may be used instead.
12+
printfn $"Abs({value}) = {Math.Abs value}"
13+
14+
// The example displays the following output:
15+
// Abs(1.79769313486232E+308) = 1.79769313486232E+308
16+
// Abs(1.6354E-16) = 1.6354E-16
17+
// Abs(15.098123) = 15.098123
18+
// Abs(0) = 0
19+
// Abs(-19.069713) = 19.069713
20+
// Abs(-1.5058E+19) = 1.5058E+19
21+
// Abs(-1.79769313486232E+308) = 1.79769313486232E+308
22+
// </Snippet2>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module abs3
2+
3+
// <Snippet3>
4+
open System
5+
6+
let values =
7+
[ Int16.MaxValue; 10328s; 0s; -1476s; Int16.MinValue ]
8+
9+
for value in values do
10+
try
11+
// The 'abs' function may be used instead.
12+
printfn $"Abs({value}) = {Math.Abs value}"
13+
with :? OverflowException ->
14+
printfn $"Unable to calculate the absolute value of {value}."
15+
16+
// The example displays the following output:
17+
// Abs(32767) = 32767
18+
// Abs(10328) = 10328
19+
// Abs(0) = 0
20+
// Abs(-1476) = 1476
21+
// Unable to calculate the absolute value of -32768.
22+
// </Snippet3>

0 commit comments

Comments
 (0)