Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.Math F# snippets #7789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions snippets/fsharp/System/Decimal/Round/fs.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Round1.fs" />
<Compile Include="Round12.fs" />
<Compile Include="mean1.fs" />
<Compile Include="midpoint1.fs" />
<Compile Include="midpoint2.fs" />
<Compile Include="mpr.fs" />
<Compile Include="precision1.fs" />
<Compile Include="precision2.fs" />
<Compile Include="Round1.fs" />
<Compile Include="Round12.fs" />
<Compile Include="Round12.fs" />
<Compile Include="single1.fs" />
<Compile Include="source.fs" />
</ItemGroup>
</Project>
33 changes: 33 additions & 0 deletions snippets/fsharp/System/Decimal/Round/mean1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module mean1

// <Snippet2>
open System

let values = [| 1.15m; 1.25m; 1.35m; 1.45m; 1.55m; 1.65m |]
let mutable sum = 0m

// Calculate true mean.
for value in values do
sum <- sum + value

printfn $"True mean: {sum / decimal values.Length:N2}"

// Calculate mean with rounding away from zero.
sum <- 0m
for value in values do
sum <- sum + Math.Round(value, 1, MidpointRounding.AwayFromZero)

printfn $"AwayFromZero: {sum / decimal values.Length:N2}"

// Calculate mean with rounding to nearest.
sum <- 0m
for value in values do
sum <- sum + Math.Round(value, 1, MidpointRounding.ToEven)

printfn $"ToEven: {sum / decimal values.Length:N2}"

// The example displays the following output:
// True mean: 1.40
// AwayFromZero: 1.45
// ToEven: 1.40
// </Snippet2>
32 changes: 32 additions & 0 deletions snippets/fsharp/System/Decimal/Round/midpoint2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module midpoint2

// <Snippet6>
open System

let values =
[| 12.; 12.1; 12.2; 12.3; 12.4; 12.5
12.6; 12.7; 12.8; 12.9; 13. |]

printfn "%-10s %-10s %-10s %-15s %-15s" "Value" "Default" "ToEven" "AwayFromZero" "ToZero"

for value in values do
$"{value,-10:R} {Math.Round(value),-10} " +
$"{Math.Round(value, MidpointRounding.ToEven),-10} " +
$"{Math.Round(value, MidpointRounding.AwayFromZero),-15} " +
$"{Math.Round(value, MidpointRounding.ToZero),-15}"
|> printfn "%s"

// The example displays the following output:
// Value Default ToEven AwayFromZero ToZero
// 12 12 12 12 12
// 12.1 12 12 12 12
// 12.2 12 12 12 12
// 12.3 12 12 12 12
// 12.4 12 12 12 12
// 12.5 12 12 13 12
// 12.6 13 13 13 12
// 12.7 13 13 13 12
// 12.8 13 13 13 12
// 12.9 13 13 13 12
// 13 13 13 13 13
// </Snippet6>
32 changes: 32 additions & 0 deletions snippets/fsharp/System/Decimal/Round/precision1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module precision1

// <Snippet7>
open System

let roundValueAndAdd (value: double) =
printfn $"{value,5:N1} {value,20:R} {Math.Round(value, MidpointRounding.ToEven),12} {Math.Round(value, MidpointRounding.AwayFromZero),15}"
value + 0.1

printfn "%5s %20s %12s %15s\n" "Value" "Full Precision" "ToEven" "AwayFromZero"
let mutable value = 11.1
for _ = 0 to 5 do
value <- roundValueAndAdd value

printfn ""

value <- 11.5
roundValueAndAdd value
|> ignore

// The example displays the following output:
// Value Full Precision ToEven AwayFromZero
//
// 11.1 11.1 11 11
// 11.2 11.2 11 11
// 11.3 11.299999999999999 11 11
// 11.4 11.399999999999999 11 11
// 11.5 11.499999999999998 11 11
// 11.6 11.599999999999998 12 12
//
// 11.5 11.5 12 12
// </Snippet7>
58 changes: 58 additions & 0 deletions snippets/fsharp/System/Decimal/Round/precision2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module precision2

// <Snippet8>
open System

let roundApproximate dbl digits margin mode =
let fraction = dbl * Math.Pow(10, digits)
let value = Math.Truncate fraction
let fraction = fraction - value
if fraction = 0 then
dbl
else
let tolerance = margin * dbl
// Determine whether this is a midpoint value.
if (fraction >= 0.5 - tolerance) && (fraction <= 0.5 + tolerance) then
if mode = MidpointRounding.AwayFromZero then
(value + 1.) / Math.Pow(10, digits)
elif value % 2. <> 0 then
(value + 1.) / Math.Pow(10, digits)
else
value / Math.Pow(10, digits)
// Any remaining fractional value greater than .5 is not a midpoint value.
elif fraction > 0.5 then
(value + 1.) / Math.Pow(10, digits)
else
value / Math.Pow(10, digits)


let roundValueAndAdd value =
let tolerance = 8e-14
let round = roundApproximate value 0 tolerance

printfn $"{value,5:N1} {value,20:R} {round MidpointRounding.ToEven,12} {round MidpointRounding.AwayFromZero,15}"
value + 0.1

printfn "%5s %20s %12s %15s\n" "Value" "Full Precision" "ToEven" "AwayFromZero"
let mutable value = 11.1
for _ = 0 to 5 do
value <- roundValueAndAdd value

printfn ""

value <- 11.5
roundValueAndAdd value
|> ignore

// The example displays the following output:
// Value Full Precision ToEven AwayFromZero
//
// 11.1 11.1 11 11
// 11.2 11.2 11 11
// 11.3 11.299999999999999 11 11
// 11.4 11.399999999999999 11 11
// 11.5 11.499999999999998 12 12
// 11.6 11.599999999999998 12 12
//
// 11.5 11.5 12 12
// </Snippet8>
27 changes: 27 additions & 0 deletions snippets/fsharp/System/Decimal/Round/single1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module single1

// <Snippet1>
// In F#, 'float', 'float64', and 'double' are aliases for System.Double...
// 'float32' and 'single' are aliases for System.Single
open System

let value = 16.325f
printfn $"Widening Conversion of {value:R} (type {value.GetType().Name}) to {double value:R} (type {(double value).GetType().Name}): "
printfn $"{Math.Round(decimal value, 2)}"
printfn $"{Math.Round(decimal value, 2, MidpointRounding.AwayFromZero)}"
printfn ""

let decValue = decimal value
printfn $"Cast of {value:R} (type {value.GetType().Name}) to {decValue} (type {decValue.GetType().Name}): "
printfn $"{Math.Round(decValue, 2)}"
printfn $"{Math.Round(decValue, 2, MidpointRounding.AwayFromZero)}"

// The example displays the following output:
// Widening Conversion of 16.325 (type Single) to 16.325000762939453 (type Double):
// 16.33
// 16.33
//
// Cast of 16.325 (type Single) to 16.325 (type Decimal):
// 16.32
// 16.33
// </Snippet1>
23 changes: 23 additions & 0 deletions snippets/fsharp/System/Decimal/Round/source.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module source

// <Snippet3>
open System

printfn
$"""{Math.Round(3.44m, 1)}
{Math.Round(3.45m, 1)}
{Math.Round(3.46m, 1)}

{Math.Round(4.34m, 1)}
{Math.Round(4.35m, 1)}
{Math.Round(4.36m, 1)}"""

// The example displays the following output:
// 3.4
// 3.4
// 3.5
//
// 4.3
// 4.4
// 4.4
// </Snippet3>
20 changes: 20 additions & 0 deletions snippets/fsharp/System/Math/Abs/Abs1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Abs1

// <Snippet1>
open System

let decimals =
[ Decimal.MaxValue; 12.45M; 0M
-19.69M; Decimal.MinValue ]

for value in decimals do
// The 'abs' function may be used instead.
printfn $"Abs({value}) = {Math.Abs value}"

// The example displays the following output:
// Abs(79228162514264337593543950335) = 79228162514264337593543950335
// Abs(12.45) = 12.45
// Abs(0) = 0
// Abs(-19.69) = 19.69
// Abs(-79228162514264337593543950335) = 79228162514264337593543950335
// </Snippet1>
22 changes: 22 additions & 0 deletions snippets/fsharp/System/Math/Abs/abs2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module abs2

// <Snippet2>
open System

let doubles =
[ Double.MaxValue; 16.354e-17; 15.098123; 0
-19.069713; -15.058e18; Double.MinValue ]

for value in doubles do
// The 'abs' function may be used instead.
printfn $"Abs({value}) = {Math.Abs value}"

// The example displays the following output:
// Abs(1.79769313486232E+308) = 1.79769313486232E+308
// Abs(1.6354E-16) = 1.6354E-16
// Abs(15.098123) = 15.098123
// Abs(0) = 0
// Abs(-19.069713) = 19.069713
// Abs(-1.5058E+19) = 1.5058E+19
// Abs(-1.79769313486232E+308) = 1.79769313486232E+308
// </Snippet2>
22 changes: 22 additions & 0 deletions snippets/fsharp/System/Math/Abs/abs3.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module abs3

// <Snippet3>
open System

let values =
[ Int16.MaxValue; 10328s; 0s; -1476s; Int16.MinValue ]

for value in values do
try
// The 'abs' function may be used instead.
printfn $"Abs({value}) = {Math.Abs value}"
with :? OverflowException ->
printfn $"Unable to calculate the absolute value of {value}."

// The example displays the following output:
// Abs(32767) = 32767
// Abs(10328) = 10328
// Abs(0) = 0
// Abs(-1476) = 1476
// Unable to calculate the absolute value of -32768.
// </Snippet3>
22 changes: 22 additions & 0 deletions snippets/fsharp/System/Math/Abs/abs4.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module abs4

// <Snippet4>
open System

let values =
[ Int32.MaxValue; 16921; 0; -804128; Int32.MinValue ]

for value in values do
try
// The 'abs' function may be used instead.
printfn $"Abs({value}) = {Math.Abs(value)}"
with :? OverflowException ->
printfn $"Unable to calculate the absolute value of {value}."

// The example displays the following output:
// Abs(2147483647) = 2147483647
// Abs(16921) = 16921
// Abs(0) = 0
// Abs(-804128) = 804128
// Unable to calculate the absolute value of -2147483648.
// </Snippet4>
22 changes: 22 additions & 0 deletions snippets/fsharp/System/Math/Abs/abs5.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module abs5

// <Snippet5>
open System

let values =
[ Int64.MaxValue; 109013; 0; -6871982; Int64.MinValue ]

for value in values do
try
// The 'abs' function may be used instead.
printfn $"Abs({value}) = {Math.Abs value}"
with :? OverflowException ->
printfn $"Unable to calculate the absolute value of {value}."

// The example displays the following output:
// Abs(9223372036854775807) = 9223372036854775807
// Abs(109013) = 109013
// Abs(0) = 0
// Abs(-6871982) = 6871982
// Unable to calculate the absolute value of -9223372036854775808.
// </Snippet5>
22 changes: 22 additions & 0 deletions snippets/fsharp/System/Math/Abs/abs6.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module abs6

// <Snippet6>
open System

let values =
[ SByte.MaxValue; 98y; 0y; -32y; SByte.MinValue ]

for value in values do
try
// The 'abs' function may be used instead.
printfn $"Abs({value}) = {Math.Abs value}"
with :? OverflowException ->
printfn $"Unable to calculate the absolute value of {value}."

// The example displays the following output:
// Abs(127) = 127
// Abs(98) = 98
// Abs(0) = 0
// Abs(-32) = 32
// Unable to calculate the absolute value of -128.
// </Snippet6>
Loading