diff --git a/snippets/fsharp/System/Decimal/Round/fs.fsproj b/snippets/fsharp/System/Decimal/Round/fs.fsproj index d4c1c8498c3..81fe1dfc473 100644 --- a/snippets/fsharp/System/Decimal/Round/fs.fsproj +++ b/snippets/fsharp/System/Decimal/Round/fs.fsproj @@ -4,9 +4,16 @@ net6.0 - - + + + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Decimal/Round/mean1.fs b/snippets/fsharp/System/Decimal/Round/mean1.fs new file mode 100644 index 00000000000..9a2f2f73227 --- /dev/null +++ b/snippets/fsharp/System/Decimal/Round/mean1.fs @@ -0,0 +1,33 @@ +module mean1 + +// +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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Decimal/Round/midpoint2.fs b/snippets/fsharp/System/Decimal/Round/midpoint2.fs new file mode 100644 index 00000000000..24ba69daa08 --- /dev/null +++ b/snippets/fsharp/System/Decimal/Round/midpoint2.fs @@ -0,0 +1,32 @@ +module midpoint2 + +// +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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Decimal/Round/precision1.fs b/snippets/fsharp/System/Decimal/Round/precision1.fs new file mode 100644 index 00000000000..89079c8af60 --- /dev/null +++ b/snippets/fsharp/System/Decimal/Round/precision1.fs @@ -0,0 +1,32 @@ +module precision1 + +// +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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Decimal/Round/precision2.fs b/snippets/fsharp/System/Decimal/Round/precision2.fs new file mode 100644 index 00000000000..c2a3a384c9a --- /dev/null +++ b/snippets/fsharp/System/Decimal/Round/precision2.fs @@ -0,0 +1,58 @@ +module precision2 + +// +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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Decimal/Round/single1.fs b/snippets/fsharp/System/Decimal/Round/single1.fs new file mode 100644 index 00000000000..c009591876c --- /dev/null +++ b/snippets/fsharp/System/Decimal/Round/single1.fs @@ -0,0 +1,27 @@ +module single1 + +// +// 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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Decimal/Round/source.fs b/snippets/fsharp/System/Decimal/Round/source.fs new file mode 100644 index 00000000000..ae814150b3e --- /dev/null +++ b/snippets/fsharp/System/Decimal/Round/source.fs @@ -0,0 +1,23 @@ +module source + +// +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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Abs/Abs1.fs b/snippets/fsharp/System/Math/Abs/Abs1.fs new file mode 100644 index 00000000000..f8545e452ad --- /dev/null +++ b/snippets/fsharp/System/Math/Abs/Abs1.fs @@ -0,0 +1,20 @@ +module Abs1 + +// +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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Abs/abs2.fs b/snippets/fsharp/System/Math/Abs/abs2.fs new file mode 100644 index 00000000000..c7a672e7de9 --- /dev/null +++ b/snippets/fsharp/System/Math/Abs/abs2.fs @@ -0,0 +1,22 @@ +module abs2 + +// +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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Abs/abs3.fs b/snippets/fsharp/System/Math/Abs/abs3.fs new file mode 100644 index 00000000000..7c1898a7f56 --- /dev/null +++ b/snippets/fsharp/System/Math/Abs/abs3.fs @@ -0,0 +1,22 @@ +module abs3 + +// +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. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Abs/abs4.fs b/snippets/fsharp/System/Math/Abs/abs4.fs new file mode 100644 index 00000000000..69c1ef36b4c --- /dev/null +++ b/snippets/fsharp/System/Math/Abs/abs4.fs @@ -0,0 +1,22 @@ +module abs4 + +// +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. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Abs/abs5.fs b/snippets/fsharp/System/Math/Abs/abs5.fs new file mode 100644 index 00000000000..101af086952 --- /dev/null +++ b/snippets/fsharp/System/Math/Abs/abs5.fs @@ -0,0 +1,22 @@ +module abs5 + +// +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. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Abs/abs6.fs b/snippets/fsharp/System/Math/Abs/abs6.fs new file mode 100644 index 00000000000..24faf95529b --- /dev/null +++ b/snippets/fsharp/System/Math/Abs/abs6.fs @@ -0,0 +1,22 @@ +module abs6 + +// +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. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Abs/abs7.fs b/snippets/fsharp/System/Math/Abs/abs7.fs new file mode 100644 index 00000000000..6a874f1891f --- /dev/null +++ b/snippets/fsharp/System/Math/Abs/abs7.fs @@ -0,0 +1,22 @@ +module abs7 + +// +open System + +let values = + [ Single.MaxValue; 16.354e-12f; 15.098123f; 0f + -19.069713f; -15.058e17f; Single.MinValue ] + +for value in values do + // The 'abs' function may be used instead. + printfn $"Abs({value}) = {Math.Abs value}" + +// The example displays the following output: +// Abs(3.402823E+38) = 3.402823E+38 +// Abs(1.6354E-11) = 1.6354E-11 +// Abs(15.09812) = 15.09812 +// Abs(0) = 0 +// Abs(-19.06971) = 19.06971 +// Abs(-1.5058E+18) = 1.5058E+18 +// Abs(-3.402823E+38) = 3.402823E+38 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Abs/fs.fsproj b/snippets/fsharp/System/Math/Abs/fs.fsproj new file mode 100644 index 00000000000..a151b5107de --- /dev/null +++ b/snippets/fsharp/System/Math/Abs/fs.fsproj @@ -0,0 +1,16 @@ + + + Exe + net6.0 + + + + + + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Atan/atan.fs b/snippets/fsharp/System/Math/Atan/atan.fs new file mode 100644 index 00000000000..fc7e8c30b39 --- /dev/null +++ b/snippets/fsharp/System/Math/Atan/atan.fs @@ -0,0 +1,42 @@ +// +// This example demonstrates Math.Atan() +// Math.Atan2() +// Math.Tan() +// Functions 'atan', 'atan2', and 'tan' may be used instead. +open System + +[] +let main _ = + let x = 1. + let y = 2. + + // Calculate the tangent of 30 degrees. + let angle = 30. + let radians = angle * (Math.PI / 180.) + let result = Math.Tan radians + printfn $"The tangent of 30 degrees is {result}." + + // Calculate the arctangent of the previous tangent. + let radians = Math.Atan result + let angle = radians * (180. / Math.PI) + printfn $"The previous tangent is equivalent to {angle} degrees." + + // Calculate the arctangent of an angle. + + let radians = Math.Atan2(y, x) + let angle = radians * (180. / Math.PI) + + printfn + $"""The arctangent of the angle formed by the x-axis and +a vector to point ({x},{y}) is {radians}, +which is equivalent to {angle} degrees.""" + 0 + +//This example produces the following results: +// The tangent of 30 degrees is 0.577350269189626. +// The previous tangent is equivalent to 30 degrees. +// +// The arctangent of the angle formed by the x-axis and +// a vector to point (1,2) is 1.10714871779409, +// which is equivalent to 63.434948822922 degrees. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Atan/fs.fsproj b/snippets/fsharp/System/Math/Atan/fs.fsproj new file mode 100644 index 00000000000..8bcef7293f9 --- /dev/null +++ b/snippets/fsharp/System/Math/Atan/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/BigMul/bigmul.fs b/snippets/fsharp/System/Math/BigMul/bigmul.fs new file mode 100644 index 00000000000..7e8afae6d3b --- /dev/null +++ b/snippets/fsharp/System/Math/BigMul/bigmul.fs @@ -0,0 +1,17 @@ +module bigmul + +// +// This example demonstrates Math.BigMul() +open System + +let int1 = Int32.MaxValue +let int2 = Int32.MaxValue + +let longResult = Math.BigMul(int1, int2) +printfn "Calculate the product of two Int32 values:" +printfn $"{int1} * {int2} = {longResult}" + +// This example produces the following results: +// Calculate the product of two Int32 values: +// 2147483647 * 2147483647 = 4611686014132420609 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/BigMul/fs.fsproj b/snippets/fsharp/System/Math/BigMul/fs.fsproj new file mode 100644 index 00000000000..c0c924559a3 --- /dev/null +++ b/snippets/fsharp/System/Math/BigMul/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Ceiling/Ceiling1.fs b/snippets/fsharp/System/Math/Ceiling/Ceiling1.fs new file mode 100644 index 00000000000..ffc432e0842 --- /dev/null +++ b/snippets/fsharp/System/Math/Ceiling/Ceiling1.fs @@ -0,0 +1,42 @@ +open System + +let ceilingWithDecimal () = + // + // The ceil and floor functions may be used instead. + let values = + [ 7.03m; 7.64m; 0.12m; -0.12m; -7.1m; -7.6m ] + printfn " Value Ceiling Floor\n" + for value in values do + printfn $"{value,7} {Math.Ceiling value,16} {Math.Floor value,14}" + // The example displays the following output to the console: + // Value Ceiling Floor + // + // 7.03 8 7 + // 7.64 8 7 + // 0.12 1 0 + // -0.12 0 -1 + // -7.1 -7 -8 + // -7.6 -7 -8 + // + +let ceilingWithDouble () = + // + // The ceil and floor functions may be used instead. + let values = + [ 7.03; 7.64; 0.12; -0.12; -7.1; -7.6 ] + printfn " Value Ceiling Floor\n" + for value in values do + printfn $"{value,7} {Math.Ceiling value,16} {Math.Floor value,14}" + // The example displays the following output to the console: + // Value Ceiling Floor + // + // 7.03 8 7 + // 7.64 8 7 + // 0.12 1 0 + // -0.12 0 -1 + // -7.1 -7 -8 + // -7.6 -7 -8 + // +ceilingWithDecimal () +printfn "" +ceilingWithDouble () \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Ceiling/fs.fsproj b/snippets/fsharp/System/Math/Ceiling/fs.fsproj new file mode 100644 index 00000000000..f57bc14098b --- /dev/null +++ b/snippets/fsharp/System/Math/Ceiling/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Cos/fs.fsproj b/snippets/fsharp/System/Math/Cos/fs.fsproj new file mode 100644 index 00000000000..9899fb6d410 --- /dev/null +++ b/snippets/fsharp/System/Math/Cos/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Cos/sincos.fs b/snippets/fsharp/System/Math/Cos/sincos.fs new file mode 100644 index 00000000000..b9ceecb57f6 --- /dev/null +++ b/snippets/fsharp/System/Math/Cos/sincos.fs @@ -0,0 +1,143 @@ +module sincos + +// +// Example for the trigonometric Math.Sin( double ) +// and Math.Cos( double ) methods. +// In F#, the sin and cos functions may be used instead. +open System + +// Evaluate trigonometric identities with a given angle. +let useSineCosine degrees = + let angle = Math.PI * degrees / 180. + let sinAngle = Math.Sin angle + let cosAngle = Math.Cos angle + + // Evaluate sin^2(X) + cos^2(X) = 1. + printfn $""" + Math.Sin({degrees} deg) = {Math.Sin angle:E16} + Math.Cos({degrees} deg) = {Math.Cos angle:E16}""" + printfn $"(Math.Sin({degrees} deg))^2 + (Math.Cos({degrees} deg))^2 = {sinAngle * sinAngle + cosAngle * cosAngle:E16}" + + // Evaluate sin(2 * X) = 2 * sin(X) * cos(X). + printfn $" Math.Sin({2. * degrees} deg) = {Math.Sin(2. * angle):E16}" + printfn $" 2 * Math.Sin({degrees} deg) * Math.Cos({degrees} deg) = {2. * sinAngle * cosAngle:E16}" + + // Evaluate cos(2 * X) = cos^2(X) - sin^2(X). + printfn $" Math.Cos({2. * degrees} deg) = {Math.Cos(2. * angle):E16}" + printfn $"(Math.Cos({degrees} deg))^2 - (Math.Sin({degrees} deg))^2 = {cosAngle * cosAngle - sinAngle * sinAngle:E16}" + + +// Evaluate trigonometric identities that are functions of two angles. +let useTwoAngles degreesX degreesY = + let angleX = Math.PI * degreesX / 180. + let angleY = Math.PI * degreesY / 180. + + // Evaluate sin(X + Y) = sin(X) * cos(Y) + cos(X) * sin(Y). + printfn $""" + Math.Sin({degreesX} deg) * Math.Cos({degreesY} deg) + Math.Cos({degreesX} deg) * Math.Sin({degreesY} deg) = {Math.Sin angleX * Math.Cos angleY + Math.Cos angleX * Math.Sin angleY:E16}""" + printfn $" Math.Sin({degreesX + degreesY} deg) = {Math.Sin(angleX + angleY):E16}" + + // Evaluate cos(X + Y) = cos(X) * cos(Y) - sin(X) * sin(Y). + printfn + $""" Math.Cos({degreesX} deg) * Math.Cos({degreesY} deg) - + Math.Sin({degreesX} deg) * Math.Sin({degreesY} deg) = {Math.Cos angleX * Math.Cos angleY - Math.Sin angleX * Math.Sin angleY:E16}""" + printfn $" Math.Cos({degreesX + degreesY} deg) = {Math.Cos(angleX + angleY):E16}" + +// Evaluate trigonometric identities with a given angle. +let useCombinedSineCosine degrees = + let angle = Math.PI * degrees / 180. + let struct(sinAngle, cosAngle) = Math.SinCos angle + + // Evaluate sin^2(X) + cos^2(X) = 1. + printfn $"\n Math.SinCos({degrees} deg) = ({sinAngle:E16}, {cosAngle:E16})" + printfn $"(double sin, double cos) = Math.SinCos({degrees} deg)" + printfn $"sin^2 + cos^2 = {sinAngle * sinAngle + cosAngle * cosAngle:E16}" + +printfn + """This example of trigonometric +Math.Sin( double ), Math.Cos( double ), and Math.SinCos( double ) +generates the following output. + +Convert selected values for X to radians +and evaluate these trigonometric identities: + sin^2(X) + cos^2(X) = 1\n sin(2 * X) = 2 * sin(X) * cos(X) + cos(2 * X) = cos^2(X) - sin^2(X) + cos(2 * X) = cos^2(X) - sin^2(X) + +""" + +useSineCosine 15. +useSineCosine 30. +useSineCosine 45. + +printfn """ +Convert selected values for X and Y to radians +and evaluate these trigonometric identities: + sin(X + Y) = sin(X) * cos(Y) + cos(X) * sin(Y) + cos(X + Y) = cos(X) * cos(Y) - sin(X) * sin(Y) +""" + +useTwoAngles 15. 30. +useTwoAngles 30. 45. + +printfn """ +When you have calls to sin(X) and cos(X) they +can be replaced with a single call to sincos(x):""" + +useCombinedSineCosine 15. +useCombinedSineCosine 30. +useCombinedSineCosine 45. + +// This example of trigonometric Math.Sin( double ) and Math.Cos( double ) +// generates the following output. +// +// Convert selected values for X to radians +// and evaluate these trigonometric identities: +// sin^2(X) + cos^2(X) = 1 +// sin(2 * X) = 2 * sin(X) * cos(X) +// cos(2 * X) = cos^2(X) - sin^2(X) +// +// Math.Sin(15 deg) = 2.5881904510252074E-001 +// Math.Cos(15 deg) = 9.6592582628906831E-001 +// (Math.Sin(15 deg))^2 + (Math.Cos(15 deg))^2 = 1.0000000000000000E+000 +// Math.Sin(30 deg) = 4.9999999999999994E-001 +// 2 * Math.Sin(15 deg) * Math.Cos(15 deg) = 4.9999999999999994E-001 +// Math.Cos(30 deg) = 8.6602540378443871E-001 +// (Math.Cos(15 deg))^2 - (Math.Sin(15 deg))^2 = 8.6602540378443871E-001 +// +// Math.Sin(30 deg) = 4.9999999999999994E-001 +// Math.Cos(30 deg) = 8.6602540378443871E-001 +// (Math.Sin(30 deg))^2 + (Math.Cos(30 deg))^2 = 1.0000000000000000E+000 +// Math.Sin(60 deg) = 8.6602540378443860E-001 +// 2 * Math.Sin(30 deg) * Math.Cos(30 deg) = 8.6602540378443860E-001 +// Math.Cos(60 deg) = 5.0000000000000011E-001 +// (Math.Cos(30 deg))^2 - (Math.Sin(30 deg))^2 = 5.0000000000000022E-001 +// +// Math.Sin(45 deg) = 7.0710678118654746E-001 +// Math.Cos(45 deg) = 7.0710678118654757E-001 +// (Math.Sin(45 deg))^2 + (Math.Cos(45 deg))^2 = 1.0000000000000000E+000 +// Math.Sin(90 deg) = 1.0000000000000000E+000 +// 2 * Math.Sin(45 deg) * Math.Cos(45 deg) = 1.0000000000000000E+000 +// Math.Cos(90 deg) = 6.1230317691118863E-017 +// (Math.Cos(45 deg))^2 - (Math.Sin(45 deg))^2 = 2.2204460492503131E-016 +// +// Convert selected values for X and Y to radians +// and evaluate these trigonometric identities: +// sin(X + Y) = sin(X) * cos(Y) + cos(X) * sin(Y) +// cos(X + Y) = cos(X) * cos(Y) - sin(X) * sin(Y) +// +// Math.Sin(15 deg) * Math.Cos(30 deg) + +// Math.Cos(15 deg) * Math.Sin(30 deg) = 7.0710678118654746E-001 +// Math.Sin(45 deg) = 7.0710678118654746E-001 +// Math.Cos(15 deg) * Math.Cos(30 deg) - +// Math.Sin(15 deg) * Math.Sin(30 deg) = 7.0710678118654757E-001 +// Math.Cos(45 deg) = 7.0710678118654757E-001 +// +// Math.Sin(30 deg) * Math.Cos(45 deg) + +// Math.Cos(30 deg) * Math.Sin(45 deg) = 9.6592582628906831E-001 +// Math.Sin(75 deg) = 9.6592582628906820E-001 +// Math.Cos(30 deg) * Math.Cos(45 deg) - +// Math.Sin(30 deg) * Math.Sin(45 deg) = 2.5881904510252085E-001 +// Math.Cos(75 deg) = 2.5881904510252096E-001 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Cosh/fs.fsproj b/snippets/fsharp/System/Math/Cosh/fs.fsproj new file mode 100644 index 00000000000..1170e977909 --- /dev/null +++ b/snippets/fsharp/System/Math/Cosh/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Cosh/sinhcosh.fs b/snippets/fsharp/System/Math/Cosh/sinhcosh.fs new file mode 100644 index 00000000000..655ea847755 --- /dev/null +++ b/snippets/fsharp/System/Math/Cosh/sinhcosh.fs @@ -0,0 +1,113 @@ +// +// Example for the hyperbolic Math.Sinh( double ) +// and Math.Cosh( double ) methods. +// In F#, the sinh and coh functions may be used instead +open System + +// Evaluate hyperbolic identities with a given argument. +let useSinhCosh arg = + let sinhArg = Math.Sinh arg + let coshArg = Math.Cosh arg + + + // Evaluate cosh^2(X) - sinh^2(X) = 1. + printfn $""" + Math.Sinh({arg}) = {Math.Sinh arg:E16} + Math.Cosh({arg}) = {Math.Cosh arg:E16}""" + printfn $"(Math.Cosh({arg}))^2 - (Math.Sinh({arg}))^2 = {coshArg * coshArg - sinhArg * sinhArg:E16}" + + // Evaluate sinh(2 * X) = 2 * sinh(X) * cosh(X). + printfn $" Math.Sinh({2. * arg}) = {Math.Sinh(2. * arg):E16}" + printfn $" 2 * Math.Sinh({arg}) * Math.Cosh({arg}) = {2. * sinhArg * coshArg:E16}" + + // Evaluate cosh(2 * X) = cosh^2(X) + sinh^2(X). + printfn $" Math.Cosh({2. * arg}) = {Math.Cosh(2. * arg):E16}" + printfn $"(Math.Cosh({arg}))^2 + (Math.Sinh({arg}))^2 = {coshArg * coshArg + sinhArg * sinhArg:E16}" + +// Evaluate hyperbolic identities that are functions of two arguments. +let useTwoArgs argX argY = + // Evaluate sinh(X + Y) = sinh(X) * cosh(Y) + cosh(X) * sinh(Y). + printfn $""" + Math.Sinh({argX}) * Math.Cosh({argY}) + Math.Cosh({argX}) * Math.Sinh({argY}) = {Math.Sinh argX * Math.Cosh argY + Math.Cosh argX * Math.Sinh argY:E16}""" + + printfn $" Math.Sinh({argX + argY}) = {Math.Sinh(argX + argY):E16}" + + // Evaluate cosh(X + Y) = cosh(X) * cosh(Y) + sinh(X) * sinh(Y). + printfn + """Math.Cosh({argX}) * Math.Cosh({argY}) + Math.Sinh({argX}) * Math.Sinh({argY}) = {Math.Cosh argX * Math.Cosh argY + Math.Sinh argX * Math.Sinh argY:E16}""" + + printfn $" Math.Cosh({argX + argY}) = {Math.Cosh(argX + argY):E16}" + +printfn + """This example of hyperbolic Math.Sinh( double ) +and Math.Cosh( double ) +generates the following output. + +Evaluate these hyperbolic identities with selected values for X: + cosh^2(X) - sinh^2(X) = 1 + sinh(2 * X) = 2 * sinh(X) * cosh(X) + cosh(2 * X) = cosh^2(X) + sinh^2(X)""" + +useSinhCosh 0.1 +useSinhCosh 1.2 +useSinhCosh 4.9 + +printfn "\nEvaluate these hyperbolic identities with selected values for X and Y:" +printfn " sinh(X + Y) = sinh(X) * cosh(Y) + cosh(X) * sinh(Y)" +printfn " cosh(X + Y) = cosh(X) * cosh(Y) + sinh(X) * sinh(Y)" + +useTwoArgs 0.1 1.2 +useTwoArgs 1.2 4.9 + +// This example of hyperbolic Math.Sinh( double ) and Math.Cosh( double ) +// generates the following output. + +// Evaluate these hyperbolic identities with selected values for X: +// cosh^2(X) - sinh^2(X) = 1 +// sinh(2 * X) = 2 * sinh(X) * cosh(X) +// cosh(2 * X) = cosh^2(X) + sinh^2(X) + +// Math.Sinh(0.1) = 1.0016675001984403E-001 +// Math.Cosh(0.1) = 1.0050041680558035E+000 +// (Math.Cosh(0.1))^2 - (Math.Sinh(0.1))^2 = 9.9999999999999989E-001 +// Math.Sinh(0.2) = 2.0133600254109399E-001 +// 2 * Math.Sinh(0.1) * Math.Cosh(0.1) = 2.0133600254109396E-001 +// Math.Cosh(0.2) = 1.0200667556190759E+000 +// (Math.Cosh(0.1))^2 + (Math.Sinh(0.1))^2 = 1.0200667556190757E+000 + +// Math.Sinh(1.2) = 1.5094613554121725E+000 +// Math.Cosh(1.2) = 1.8106555673243747E+000 +// (Math.Cosh(1.2))^2 - (Math.Sinh(1.2))^2 = 1.0000000000000000E+000 +// Math.Sinh(2.4) = 5.4662292136760939E+000 +// 2 * Math.Sinh(1.2) * Math.Cosh(1.2) = 5.4662292136760939E+000 +// Math.Cosh(2.4) = 5.5569471669655064E+000 +// (Math.Cosh(1.2))^2 + (Math.Sinh(1.2))^2 = 5.5569471669655064E+000 + +// Math.Sinh(4.9) = 6.7141166550932297E+001 +// Math.Cosh(4.9) = 6.7148613134003227E+001 +// (Math.Cosh(4.9))^2 - (Math.Sinh(4.9))^2 = 1.0000000000000000E+000 +// Math.Sinh(9.8) = 9.0168724361884615E+003 +// 2 * Math.Sinh(4.9) * Math.Cosh(4.9) = 9.0168724361884615E+003 +// Math.Cosh(9.8) = 9.0168724916400624E+003 +// (Math.Cosh(4.9))^2 + (Math.Sinh(4.9))^2 = 9.0168724916400606E+003 + +// Evaluate these hyperbolic identities with selected values for X and Y: +// sinh(X + Y) = sinh(X) * cosh(Y) + cosh(X) * sinh(Y) +// cosh(X + Y) = cosh(X) * cosh(Y) + sinh(X) * sinh(Y) + +// Math.Sinh(0.1) * Math.Cosh(1.2) + +// Math.Cosh(0.1) * Math.Sinh(1.2) = 1.6983824372926155E+000 +// Math.Sinh(1.3) = 1.6983824372926160E+000 +// Math.Cosh(0.1) * Math.Cosh(1.2) + +// Math.Sinh(0.1) * Math.Sinh(1.2) = 1.9709142303266281E+000 +// Math.Cosh(1.3) = 1.9709142303266285E+000 + +// Math.Sinh(1.2) * Math.Cosh(4.9) + +// Math.Cosh(1.2) * Math.Sinh(4.9) = 2.2292776360739879E+002 +// Math.Sinh(6.1) = 2.2292776360739885E+002 +// Math.Cosh(1.2) * Math.Cosh(4.9) + +// Math.Sinh(1.2) * Math.Sinh(4.9) = 2.2293000647511826E+002 +// Math.Cosh(6.1) = 2.2293000647511832E+002 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/DivRem/divrem1.fs b/snippets/fsharp/System/Math/DivRem/divrem1.fs new file mode 100644 index 00000000000..4d8bcd93a3d --- /dev/null +++ b/snippets/fsharp/System/Math/DivRem/divrem1.fs @@ -0,0 +1,28 @@ +module divrem1 + +// +open System + +// Define several positive and negative dividends. +let dividends = + [ Int32.MaxValue; 13952; 0; -14032; Int32.MinValue ] +// Define one positive and one negative divisor. +let divisors = [ 2000; -2000 ] + +for divisor in divisors do + for dividend in dividends do + let quotient, remainder = Math.DivRem(dividend, divisor) + printfn $@"{dividend:N0} \ {divisor:N0} = {quotient:N0}, remainder {remainder:N0}" + +// The example displays the following output: +// 2,147,483,647 \ 2,000 = 1,073,741, remainder 1,647 +// 13,952 \ 2,000 = 6, remainder 1,952 +// 0 \ 2,000 = 0, remainder 0 +// -14,032 \ 2,000 = -7, remainder -32 +// -2,147,483,648 \ 2,000 = -1,073,741, remainder -1,648 +// 2,147,483,647 \ -2,000 = -1,073,741, remainder 1,647 +// 13,952 \ -2,000 = -6, remainder 1,952 +// 0 \ -2,000 = 0, remainder 0 +// -14,032 \ -2,000 = 7, remainder -32 +// -2,147,483,648 \ -2,000 = 1,073,741, remainder -1,648 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/DivRem/divrem2.fs b/snippets/fsharp/System/Math/DivRem/divrem2.fs new file mode 100644 index 00000000000..ca2d319a982 --- /dev/null +++ b/snippets/fsharp/System/Math/DivRem/divrem2.fs @@ -0,0 +1,28 @@ +module divrem2 + +// +open System + +// Define several positive and negative dividends. +let dividends = + [ Int64.MaxValue; 13952; 0; -14032; Int64.MinValue ] +// Define one positive and one negative divisor. +let divisors = [ 2000; -2000 ] + +for divisor in divisors do + for dividend in dividends do + let quotient, remainder = Math.DivRem(dividend, divisor) + printfn $@"{dividend:N0} \ {divisor:N0} = {quotient:N0}, remainder {remainder:N0}" + +// The example displays the following output: +// 9,223,372,036,854,775,807 \ 2,000 = 4,611,686,018,427,387, remainder 1,807 +// 13,952 \ 2,000 = 6, remainder 1,952 +// 0 \ 2,000 = 0, remainder 0 +// -14,032 \ 2,000 = -7, remainder -32 +// -9,223,372,036,854,775,808 \ 2,000 = -4,611,686,018,427,387, remainder -1,808 +// 9,223,372,036,854,775,807 \ -2,000 = -4,611,686,018,427,387, remainder 1,807 +// 13,952 \ -2,000 = -6, remainder 1,952 +// 0 \ -2,000 = 0, remainder 0 +// -14,032 \ -2,000 = 7, remainder -32 +// -9,223,372,036,854,775,808 \ -2,000 = 4,611,686,018,427,387, remainder -1,808 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/DivRem/fs.fsproj b/snippets/fsharp/System/Math/DivRem/fs.fsproj new file mode 100644 index 00000000000..b816b88a9c5 --- /dev/null +++ b/snippets/fsharp/System/Math/DivRem/fs.fsproj @@ -0,0 +1,11 @@ + + + Exe + net6.0 + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/E/efield.fs b/snippets/fsharp/System/Math/E/efield.fs new file mode 100644 index 00000000000..d864ee86ccf --- /dev/null +++ b/snippets/fsharp/System/Math/E/efield.fs @@ -0,0 +1,54 @@ +// +// Example for the Math.E field. +open System + +// Approximate E with a power series. +let calcPowerSeries () = + let mutable factorial = 1. + let mutable PS = 0. + let mutable n = 0 + // Stop iterating when the series converges, + // and prevent a runaway process. + while n < 999 && abs (Math.E - PS) > 1.0E-15 do + // Calculate a running factorial. + if n > 0 then + factorial <- factorial * double n + + // Calculate and display the power series. + PS <- PS + 1. / factorial + printfn $"PS({n:D2}) = {PS:E16}, Math.E - PS({n:D2}) = {Math.E - PS:E16}" + n <- n + 1 + +printfn $"This example of Math.E = {Math.E:E16}\ngenerates the following output.\n" +printfn "Define the power series PS(n) = Sum(k->0,n)[1/k!]" +printfn " (limit n->infinity)PS(n) = e" +printfn "Display PS(n) and Math.E - PS(n), and stop when delta < 1.0E-15\n" + +calcPowerSeries () + +// This example of Math.E = 2.7182818284590451E+000 +// generates the following output. +// +// Define the power series PS(n) = Sum(k->0,n)[1/k!] +// (limit n->infinity)PS(n) = e +// Display PS(n) and Math.E - PS(n), and stop when delta < 1.0E-15 +// +// PS(00) = 1.0000000000000000E+000, Math.E - PS(00) = 1.7182818284590451E+000 +// PS(01) = 2.0000000000000000E+000, Math.E - PS(01) = 7.1828182845904509E-001 +// PS(02) = 2.5000000000000000E+000, Math.E - PS(02) = 2.1828182845904509E-001 +// PS(03) = 2.6666666666666665E+000, Math.E - PS(03) = 5.1615161792378572E-002 +// PS(04) = 2.7083333333333330E+000, Math.E - PS(04) = 9.9484951257120535E-003 +// PS(05) = 2.7166666666666663E+000, Math.E - PS(05) = 1.6151617923787498E-003 +// PS(06) = 2.7180555555555554E+000, Math.E - PS(06) = 2.2627290348964380E-004 +// PS(07) = 2.7182539682539684E+000, Math.E - PS(07) = 2.7860205076724043E-005 +// PS(08) = 2.7182787698412700E+000, Math.E - PS(08) = 3.0586177750535626E-006 +// PS(09) = 2.7182815255731922E+000, Math.E - PS(09) = 3.0288585284310443E-007 +// PS(10) = 2.7182818011463845E+000, Math.E - PS(10) = 2.7312660577649694E-008 +// PS(11) = 2.7182818261984929E+000, Math.E - PS(11) = 2.2605521898810821E-009 +// PS(12) = 2.7182818282861687E+000, Math.E - PS(12) = 1.7287637987806193E-010 +// PS(13) = 2.7182818284467594E+000, Math.E - PS(13) = 1.2285727990501982E-011 +// PS(14) = 2.7182818284582302E+000, Math.E - PS(14) = 8.1490370007486490E-013 +// PS(15) = 2.7182818284589949E+000, Math.E - PS(15) = 5.0182080713057076E-014 +// PS(16) = 2.7182818284590429E+000, Math.E - PS(16) = 2.2204460492503131E-015 +// PS(17) = 2.7182818284590455E+000, Math.E - PS(17) = -4.4408920985006262E-016 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/E/fs.fsproj b/snippets/fsharp/System/Math/E/fs.fsproj new file mode 100644 index 00000000000..0843c06f25b --- /dev/null +++ b/snippets/fsharp/System/Math/E/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Exp/exp.fs b/snippets/fsharp/System/Math/Exp/exp.fs new file mode 100644 index 00000000000..a6e48de8e72 --- /dev/null +++ b/snippets/fsharp/System/Math/Exp/exp.fs @@ -0,0 +1,82 @@ +// +// Example for the Math.Exp( double ) method. +// The exp function may be used instead. + +open System +printfn "This example of Math.Exp( double ) generates the following output.\n" +printfn "Evaluate [e ^ ln(X) = ln(e ^ X) = X] with selected values for X:" + +// Evaluate logarithmic/exponential identity with a given argument. +let useLnExp arg = + // Evaluate e ^ ln(X) = ln(e ^ X) = X. + printfn $"\n Math.Exp(Math.Log({arg})) = {Math.Exp(Math.Log arg):E16}\n Math.Log(Math.Exp({arg})) = {Math.Log(Math.Exp arg):E16}" + +// Evaluate exponential identities that are functions of two arguments. +let useTwoArgs argX argY = + // Evaluate (e ^ X) * (e ^ Y) = e ^ (X + Y). + printfn $""" +Math.Exp({argX}) * Math.Exp({argY}) = {Math.Exp argX * Math.Exp argY:E16}" + + Math.Exp({argX} + {argY}) = {Math.Exp(argX + argY):E16}""" + + // Evaluate (e ^ X) ^ Y = e ^ (X * Y). + printfn $" Math.Pow(Math.Exp({argX}), {argY}) = {Math.Pow(Math.Exp argX, argY):E16}\n Math.Exp({argX} * {argY}) = {Math.Exp(argX * argY):E16}" + + // Evaluate X ^ Y = e ^ (Y * ln(X)). + printfn $" Math.Pow({argX}, {argY}) = {Math.Pow(argX, argY):E16}\nMath.Exp({argY} * Math.Log({argX})) = {Math.Exp(argY * Math.Log argX):E16}" + +useLnExp 0.1 +useLnExp 1.2 +useLnExp 4.9 +useLnExp 9.9 + +printfn "\nEvaluate these identities with selected values for X and Y:" +printfn " (e ^ X) * (e ^ Y) = e ^ (X + Y)" +printfn " (e ^ X) ^ Y = e ^ (X * Y)" +printfn " X ^ Y = e ^ (Y * ln(X))" + +useTwoArgs 0.1 1.2 +useTwoArgs 1.2 4.9 +useTwoArgs 4.9 9.9 + +// This example of Math.Exp( double ) generates the following output. +// +// Evaluate [e ^ ln(X) = ln(e ^ X) = X] with selected values for X: +// +// Math.Exp(Math.Log(0.1)) = 1.0000000000000001E-001 +// Math.Log(Math.Exp(0.1)) = 1.0000000000000008E-001 +// +// Math.Exp(Math.Log(1.2)) = 1.2000000000000000E+000 +// Math.Log(Math.Exp(1.2)) = 1.2000000000000000E+000 +// +// Math.Exp(Math.Log(4.9)) = 4.9000000000000012E+000 +// Math.Log(Math.Exp(4.9)) = 4.9000000000000004E+000 +// +// Math.Exp(Math.Log(9.9)) = 9.9000000000000004E+000 +// Math.Log(Math.Exp(9.9)) = 9.9000000000000004E+000 +// +// Evaluate these identities with selected values for X and Y: +// (e ^ X) * (e ^ Y) = e ^ (X + Y) +// (e ^ X) ^ Y = e ^ (X * Y) +// X ^ Y = e ^ (Y * ln(X)) +// +// Math.Exp(0.1) * Math.Exp(1.2) = 3.6692966676192444E+000 +// Math.Exp(0.1 + 1.2) = 3.6692966676192444E+000 +// Math.Pow(Math.Exp(0.1), 1.2) = 1.1274968515793757E+000 +// Math.Exp(0.1 * 1.2) = 1.1274968515793757E+000 +// Math.Pow(0.1, 1.2) = 6.3095734448019331E-002 +// Math.Exp(1.2 * Math.Log(0.1)) = 6.3095734448019344E-002 +// +// Math.Exp(1.2) * Math.Exp(4.9) = 4.4585777008251705E+002 +// Math.Exp(1.2 + 4.9) = 4.4585777008251716E+002 +// Math.Pow(Math.Exp(1.2), 4.9) = 3.5780924170885260E+002 +// Math.Exp(1.2 * 4.9) = 3.5780924170885277E+002 +// Math.Pow(1.2, 4.9) = 2.4433636334442981E+000 +// Math.Exp(4.9 * Math.Log(1.2)) = 2.4433636334442981E+000 +// +// Math.Exp(4.9) * Math.Exp(9.9) = 2.6764450551890982E+006 +// Math.Exp(4.9 + 9.9) = 2.6764450551891015E+006 +// Math.Pow(Math.Exp(4.9), 9.9) = 1.1684908531676833E+021 +// Math.Exp(4.9 * 9.9) = 1.1684908531676829E+021 +// Math.Pow(4.9, 9.9) = 6.8067718210957060E+006 +// Math.Exp(9.9 * Math.Log(4.9)) = 6.8067718210956985E+006 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Exp/fs.fsproj b/snippets/fsharp/System/Math/Exp/fs.fsproj new file mode 100644 index 00000000000..3054bf0aa01 --- /dev/null +++ b/snippets/fsharp/System/Math/Exp/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/IEEERemainder/fs.fsproj b/snippets/fsharp/System/Math/IEEERemainder/fs.fsproj new file mode 100644 index 00000000000..acf0656ba69 --- /dev/null +++ b/snippets/fsharp/System/Math/IEEERemainder/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/IEEERemainder/ieeeremainder1.fs b/snippets/fsharp/System/Math/IEEERemainder/ieeeremainder1.fs new file mode 100644 index 00000000000..2a9211f2922 --- /dev/null +++ b/snippets/fsharp/System/Math/IEEERemainder/ieeeremainder1.fs @@ -0,0 +1,38 @@ +// +open System + +let showRemainders number1 number2 = + let formula = $"{number1} / {number2} = " + let ieeeRemainder = Math.IEEERemainder(number1, number2) + let remainder = number1 % number2 + printfn $"{formula,-16} {ieeeRemainder,18} {remainder,20}" + +printfn " IEEERemainder Remainder operator" +showRemainders 3 2 +showRemainders 4 2 +showRemainders 10 3 +showRemainders 11 3 +showRemainders 27 4 +showRemainders 28 5 +showRemainders 17.8 4 +showRemainders 17.8 4.1 +showRemainders -16.3 4.1 +showRemainders 17.8 -4.1 +showRemainders -17.8 -4.1 + +// The example displays the following output: +// +// +// IEEERemainder Remainder operator +// 3 / 2 = -1 1 +// 4 / 2 = 0 0 +// 10 / 3 = 1 1 +// 11 / 3 = -1 2 +// 27 / 4 = -1 3 +// 28 / 5 = -2 3 +// 17.8 / 4 = 1.8 1.8 +// 17.8 / 4.1 = 1.4 1.4 +// -16.3 / 4.1 = 0.0999999999999979 -4 +// 17.8 / -4.1 = 1.4 1.4 +// -17.8 / -4.1 = -1.4 -1.4 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Log10/fs.fsproj b/snippets/fsharp/System/Math/Log10/fs.fsproj new file mode 100644 index 00000000000..66a53cf0f9b --- /dev/null +++ b/snippets/fsharp/System/Math/Log10/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Log10/log10.fs b/snippets/fsharp/System/Math/Log10/log10.fs new file mode 100644 index 00000000000..a99a57c9166 --- /dev/null +++ b/snippets/fsharp/System/Math/Log10/log10.fs @@ -0,0 +1,26 @@ +// +open System + +let numbers = + [ -1.; 0; 0.105; 0.5; 0.798; 1; 4; 6.9; 10 + 50; 100; 500; 1000; Double.MaxValue ] + +for number in numbers do + // the F# log10 function may be used instead + printfn $"The base 10 log of {number} is {Math.Log10 number}." +// The example dislays the following output: +// The base 10 log of -1 is NaN. +// The base 10 log of 0 is -Infinity. +// The base 10 log of 0.105 is -0.978810700930062. +// The base 10 log of 0.5 is -0.301029995663981. +// The base 10 log of 0.798 is -0.0979971086492706. +// The base 10 log of 1 is 0. +// The base 10 log of 4 is 0.602059991327962. +// The base 10 log of 6.9 is 0.838849090737255. +// The base 10 log of 10 is 1. +// The base 10 log of 50 is 1.69897000433602. +// The base 10 log of 100 is 2. +// The base 10 log of 500 is 2.69897000433602. +// The base 10 log of 1000 is 3. +// The base 10 log of 1.79769313486232E+308 is 308.254715559917. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/LogMethod/fs.fsproj b/snippets/fsharp/System/Math/LogMethod/fs.fsproj new file mode 100644 index 00000000000..9ff9036710f --- /dev/null +++ b/snippets/fsharp/System/Math/LogMethod/fs.fsproj @@ -0,0 +1,11 @@ + + + Exe + net6.0 + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/LogMethod/log1.fs b/snippets/fsharp/System/Math/LogMethod/log1.fs new file mode 100644 index 00000000000..5d73a08875e --- /dev/null +++ b/snippets/fsharp/System/Math/LogMethod/log1.fs @@ -0,0 +1,34 @@ +module log1 + +// +open System + +printfn " Evaluate this identity with selected values for X:" +printfn " ln(x) = 1 / log[X](B)\n" + +let XArgs = [| 1.2; 4.9; 9.9; 0.1 |] + +for argX in XArgs do + // Find natural log of argX. + // The F# log function may be used instead + printfn $" Math.Log({argX}) = {Math.Log argX:E16}" + + // Evaluate 1 / log[X](e). + printfn $" 1.0 / Math.Log(e, {argX}) = {1. / Math.Log(Math.E, argX):E16}\n" + +// This example displays the following output: +// Evaluate this identity with selected values for X: +// ln(x) = 1 / log[X](B) +// +// Math.Log(1.2) = 1.8232155679395459E-001 +// 1.0 / Math.Log(e, 1.2) = 1.8232155679395459E-001 +// +// Math.Log(4.9) = 1.5892352051165810E+000 +// 1.0 / Math.Log(e, 4.9) = 1.5892352051165810E+000 +// +// Math.Log(9.9) = 2.2925347571405443E+000 +// 1.0 / Math.Log(e, 9.9) = 2.2925347571405443E+000 +// +// Math.Log(0.1) = -2.3025850929940455E+000 +// 1.0 / Math.Log(e, 0.1) = -2.3025850929940455E+000 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/LogMethod/loggen.fs b/snippets/fsharp/System/Math/LogMethod/loggen.fs new file mode 100644 index 00000000000..e151df9e22b --- /dev/null +++ b/snippets/fsharp/System/Math/LogMethod/loggen.fs @@ -0,0 +1,63 @@ +module loggen + +// +// Example for the Math.Log( double ) and Math.Log( double, double ) methods. +open System + +// Evaluate logarithmic identities that are functions of two arguments. +let useBaseAndArg argB argX = + // Evaluate log(B)[X] == 1 / log(X)[B]. + printfn $""" + Math.Log({argX}, {argB}) == {Math.Log(argX, argB):E16} + 1.0 / Math.Log({argB}, {argX}) == {1. / Math.Log(argB, argX):E16}""" + + // Evaluate log(B)[X] == ln[X] / ln[B]. + printfn $" Math.Log({argX}) / Math.Log({argB}) == {Math.Log argX / Math.Log argB:E16}" + + // Evaluate log(B)[X] == log(B)[e] * ln[X]. + printfn $"Math.Log(Math.E, {argB}) * Math.Log({argX}) == {Math.Log(Math.E, argB) * Math.Log argX:E16}" + + +printfn + """This example of Math.Log( double ) and Math.Log( double, double ) +generates the following output. + +printfn "Evaluate these identities with selected values for X and B (base):""" +printfn " log(B)[X] == 1 / log(X)[B]" +printfn " log(B)[X] == ln[X] / ln[B]" +printfn " log(B)[X] == log(B)[e] * ln[X]" + +useBaseAndArg 0.1 1.2 +useBaseAndArg 1.2 4.9 +useBaseAndArg 4.9 9.9 +useBaseAndArg 9.9 0.1 + + +// This example of Math.Log( double ) and Math.Log( double, double ) +// generates the following output. +// +// Evaluate these identities with selected values for X and B (base): +// log(B)[X] == 1 / log(X)[B] +// log(B)[X] == ln[X] / ln[B] +// log(B)[X] == log(B)[e] * ln[X] +// +// Math.Log(1.2, 0.1) == -7.9181246047624818E-002 +// 1.0 / Math.Log(0.1, 1.2) == -7.9181246047624818E-002 +// Math.Log(1.2) / Math.Log(0.1) == -7.9181246047624818E-002 +// Math.Log(Math.E, 0.1) * Math.Log(1.2) == -7.9181246047624804E-002 +// +// Math.Log(4.9, 1.2) == 8.7166610085093179E+000 +// 1.0 / Math.Log(1.2, 4.9) == 8.7166610085093161E+000 +// Math.Log(4.9) / Math.Log(1.2) == 8.7166610085093179E+000 +// Math.Log(Math.E, 1.2) * Math.Log(4.9) == 8.7166610085093179E+000 +// +// Math.Log(9.9, 4.9) == 1.4425396251981288E+000 +// 1.0 / Math.Log(4.9, 9.9) == 1.4425396251981288E+000 +// Math.Log(9.9) / Math.Log(4.9) == 1.4425396251981288E+000 +// Math.Log(Math.E, 4.9) * Math.Log(9.9) == 1.4425396251981288E+000 +// +// Math.Log(0.1, 9.9) == -1.0043839404494075E+000 +// 1.0 / Math.Log(9.9, 0.1) == -1.0043839404494075E+000 +// Math.Log(0.1) / Math.Log(9.9) == -1.0043839404494075E+000 +// Math.Log(Math.E, 9.9) * Math.Log(0.1) == -1.0043839404494077E+000 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Max/fs.fsproj b/snippets/fsharp/System/Math/Max/fs.fsproj new file mode 100644 index 00000000000..5e007e0b75a --- /dev/null +++ b/snippets/fsharp/System/Math/Max/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Max/max.fs b/snippets/fsharp/System/Math/Max/max.fs new file mode 100644 index 00000000000..3ad4ca7464c --- /dev/null +++ b/snippets/fsharp/System/Math/Max/max.fs @@ -0,0 +1,54 @@ +// This example demonstrates Math.Max() +// +open System + +let print typ a b m = + printfn $"{typ}: The greater of {a,3} and {b,3} is {m}." + +let xByte1, xByte2 = 1uy, 51uy +let xShort1, xShort2 = -2s, 52s +let xInt1, xInt2 = -3, 53 +let xLong1, xLong2 = -4L, 54L +let xSingle1, xSingle2 = 5f, 55f +let xDouble1, xDouble2 = 6., 56. +let xDecimal1, xDecimal2 = 7m, 57m + +// The following types are not CLS-compliant. +let xSbyte1, xSbyte2 = 101y, 111y +let xUshort1, xUshort2 = 102us, 112us +let xUint1, xUint2 = 103u, 113u +let xUlong1, xUlong2 = 104uL, 114uL + +printfn "Display the greater of two values:\n" +print "Byte " xByte1 xByte2 (Math.Max(xByte1, xByte2)) +print "Int16 " xShort1 xShort2 (Math.Max(xShort1, xShort2)) +print "Int32 " xInt1 xInt2 (Math.Max(xInt1, xInt2)) +print "Int64 " xLong1 xLong2 (Math.Max(xLong1, xLong2)) +print "Single " xSingle1 xSingle2 (Math.Max(xSingle1, xSingle2)) +print "Double " xDouble1 xDouble2 (Math.Max(xDouble1, xDouble2)) +print "Decimal" xDecimal1 xDecimal2 (Math.Max(xDecimal1, xDecimal2)) + +printfn "\nThe following types are not CLS-compliant.\n" +print "SByte " xSbyte1 xSbyte2 (Math.Max(xSbyte1, xSbyte2)) +print "UInt16 " xUshort1 xUshort2 (Math.Max(xUshort1, xUshort2)) +print "UInt32 " xUint1 xUint2 (Math.Max(xUint1, xUint2)) +print "UInt64 " xUlong1 xUlong2 (Math.Max(xUlong1, xUlong2)) + +// This example produces the following results: +// Display the greater of two values: +// +// Byte : The greater of 1 and 51 is 51. +// Int16 : The greater of -2 and 52 is 52. +// Int32 : The greater of -3 and 53 is 53. +// Int64 : The greater of -4 and 54 is 54. +// Single : The greater of 5 and 55 is 55. +// Double : The greater of 6 and 56 is 56. +// Decimal: The greater of 7 and 57 is 57. +// +// (The following types are not CLS-compliant.) +// +// SByte : The greater of 101 and 111 is 111. +// UInt16 : The greater of 102 and 112 is 112. +// UInt32 : The greater of 103 and 113 is 113. +// UInt64 : The greater of 104 and 114 is 114. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Min/fs.fsproj b/snippets/fsharp/System/Math/Min/fs.fsproj new file mode 100644 index 00000000000..b8ecea51b88 --- /dev/null +++ b/snippets/fsharp/System/Math/Min/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Min/min.fs b/snippets/fsharp/System/Math/Min/min.fs new file mode 100644 index 00000000000..39334f105ff --- /dev/null +++ b/snippets/fsharp/System/Math/Min/min.fs @@ -0,0 +1,54 @@ +// This example demonstrates Math.Min() +// +open System + +let print typ a b m = + printfn $"{typ}: The lesser of {a,3} and {b,3} is {m}." + +let xByte1, xByte2 = 1uy, 51uy +let xShort1, xShort2 = -2s, 52s +let xInt1, xInt2 = -3, 53 +let xLong1, xLong2 = -4L, 54L +let xSingle1, xSingle2 = 5f, 55f +let xDouble1, xDouble2 = 6., 56. +let xDecimal1, xDecimal2 = 7m, 57m + +// The following types are not CLS-compliant. +let xSbyte1, xSbyte2 = 101y, 111y +let xUshort1, xUshort2 = 102us, 112us +let xUint1, xUint2 = 103u, 113u +let xUlong1, xUlong2 = 104uL, 114uL + +printfn "Display the lesser of two values:\n" +print "Byte " xByte1 xByte2 (Math.Min(xByte1, xByte2)) +print "Int16 " xShort1 xShort2 (Math.Min(xShort1, xShort2)) +print "Int32 " xInt1 xInt2 (Math.Min(xInt1, xInt2)) +print "Int64 " xLong1 xLong2 (Math.Min(xLong1, xLong2)) +print "Single " xSingle1 xSingle2 (Math.Min(xSingle1, xSingle2)) +print "Double " xDouble1 xDouble2 (Math.Min(xDouble1, xDouble2)) +print "Decimal" xDecimal1 xDecimal2 (Math.Min(xDecimal1, xDecimal2)) + +printfn"\nThe following types are not CLS-compliant:\n" +print "SByte " xSbyte1 xSbyte2 (Math.Min(xSbyte1, xSbyte2)) +print "UInt16 " xUshort1 xUshort2 (Math.Min(xUshort1, xUshort2)) +print "UInt32 " xUint1 xUint2 (Math.Min(xUint1, xUint2)) +print "UInt64 " xUlong1 xUlong2 (Math.Min(xUlong1, xUlong2)) + +// This example produces the following results: +// Display the lesser of two values: +// +// Byte : The lesser of 1 and 51 is 1. +// Int16 : The lesser of -2 and 52 is -2. +// Int32 : The lesser of -3 and 53 is -3. +// Int64 : The lesser of -4 and 54 is -4. +// Single : The lesser of 5 and 55 is 5. +// Double : The lesser of 6 and 56 is 6. +// Decimal: The lesser of 7 and 57 is 7. +// +// The following types are not CLS-compliant: +// +// SByte : The lesser of 101 and 111 is 101. +// UInt16 : The lesser of 102 and 112 is 102. +// UInt32 : The lesser of 103 and 113 is 103. +// UInt64 : The lesser of 104 and 114 is 104. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Overview/fs.fsproj b/snippets/fsharp/System/Math/Overview/fs.fsproj new file mode 100644 index 00000000000..e7c24379e40 --- /dev/null +++ b/snippets/fsharp/System/Math/Overview/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Overview/mathsample.fs b/snippets/fsharp/System/Math/Overview/mathsample.fs new file mode 100644 index 00000000000..fff3c13d9de --- /dev/null +++ b/snippets/fsharp/System/Math/Overview/mathsample.fs @@ -0,0 +1,45 @@ +// +open System + +/// The following class represents simple functionality of the trapezoid. +type MathTrapezoidSample(longbase, shortbase, leftLeg, rightLeg) = + member _.GetRightSmallBase() = + (Math.Pow(rightLeg, 2.) - Math.Pow(leftLeg, 2.) + Math.Pow(longbase, 2.) + Math.Pow(shortbase, 2.) - 2. * shortbase * longbase) / (2. * (longbase - shortbase)) + + member this.GetHeight() = + let x = this.GetRightSmallBase() + Math.Sqrt(Math.Pow(rightLeg, 2.) - Math.Pow(x, 2.)) + + member this.GetSquare() = + this.GetHeight() * longbase / 2. + + member this.GetLeftBaseRadianAngle() = + let sinX = this.GetHeight() / leftLeg + Math.Round(Math.Asin sinX,2) + + member this.GetRightBaseRadianAngle() = + let x = this.GetRightSmallBase() + let cosX = (Math.Pow(rightLeg, 2.) + Math.Pow(x, 2.) - Math.Pow(this.GetHeight(), 2.))/(2. * x * rightLeg) + Math.Round(Math.Acos cosX, 2) + + member this.GetLeftBaseDegreeAngle() = + let x = this.GetLeftBaseRadianAngle() * 180. / Math.PI + Math.Round(x, 2) + + member this.GetRightBaseDegreeAngle() = + let x = this.GetRightBaseRadianAngle() * 180. / Math.PI + Math.Round(x, 2) + +let trpz = MathTrapezoidSample(20., 10., 8., 6.) +printfn "The trapezoid's bases are 20.0 and 10.0, the trapezoid's legs are 8.0 and 6.0" +let h = trpz.GetHeight() +printfn $"Trapezoid height is: {h}" +let dxR = trpz.GetLeftBaseRadianAngle() +printfn $"Trapezoid left base angle is: {dxR} Radians" +let dyR = trpz.GetRightBaseRadianAngle() +printfn $"Trapezoid right base angle is: {dyR} Radians" +let dxD = trpz.GetLeftBaseDegreeAngle() +printfn $"Trapezoid left base angle is: {dxD} Degrees" +let dyD = trpz.GetRightBaseDegreeAngle() +printfn $"Trapezoid left base angle is: {dyD} Degrees" +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Pow/fs.fsproj b/snippets/fsharp/System/Math/Pow/fs.fsproj new file mode 100644 index 00000000000..3fb6e06833d --- /dev/null +++ b/snippets/fsharp/System/Math/Pow/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Pow/pow1.fs b/snippets/fsharp/System/Math/Pow/pow1.fs new file mode 100644 index 00000000000..cf4d9abb21b --- /dev/null +++ b/snippets/fsharp/System/Math/Pow/pow1.fs @@ -0,0 +1,42 @@ +// +open System + +let value = 2 +for power = 0 to 32 do + printfn $"{value}^{power} = {Math.Pow(value, power) |> int64:N0} (0x{Math.Pow(value, power) |> int64:X})" + +// The example displays the following output: +// 2^0 = 1 (0x1) +// 2^1 = 2 (0x2) +// 2^2 = 4 (0x4) +// 2^3 = 8 (0x8) +// 2^4 = 16 (0x10) +// 2^5 = 32 (0x20) +// 2^6 = 64 (0x40) +// 2^7 = 128 (0x80) +// 2^8 = 256 (0x100) +// 2^9 = 512 (0x200) +// 2^10 = 1,024 (0x400) +// 2^11 = 2,048 (0x800) +// 2^12 = 4,096 (0x1000) +// 2^13 = 8,192 (0x2000) +// 2^14 = 16,384 (0x4000) +// 2^15 = 32,768 (0x8000) +// 2^16 = 65,536 (0x10000) +// 2^17 = 131,072 (0x20000) +// 2^18 = 262,144 (0x40000) +// 2^19 = 524,288 (0x80000) +// 2^20 = 1,048,576 (0x100000) +// 2^21 = 2,097,152 (0x200000) +// 2^22 = 4,194,304 (0x400000) +// 2^23 = 8,388,608 (0x800000) +// 2^24 = 16,777,216 (0x1000000) +// 2^25 = 33,554,432 (0x2000000) +// 2^26 = 67,108,864 (0x4000000) +// 2^27 = 134,217,728 (0x8000000) +// 2^28 = 268,435,456 (0x10000000) +// 2^29 = 536,870,912 (0x20000000) +// 2^30 = 1,073,741,824 (0x40000000) +// 2^31 = 2,147,483,648 (0x80000000) +// 2^32 = 4,294,967,296 (0x100000000) +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Round/fs.fsproj b/snippets/fsharp/System/Math/Round/fs.fsproj new file mode 100644 index 00000000000..7a9cb1cfc66 --- /dev/null +++ b/snippets/fsharp/System/Math/Round/fs.fsproj @@ -0,0 +1,16 @@ + + + Exe + net6.0 + + + + + + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Round/round2.fs b/snippets/fsharp/System/Math/Round/round2.fs new file mode 100644 index 00000000000..f223f3505b7 --- /dev/null +++ b/snippets/fsharp/System/Math/Round/round2.fs @@ -0,0 +1,30 @@ +module round2 + +// +open System + +let roundValueAndAdd (value: float) = + printfn $"{value} --> {Math.Round value}" + value + 0.1 + +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: +// 11.1 --> 11 +// 11.2 --> 11 +// 11.3 --> 11 +// 11.4 --> 11 +// 11.5 --> 11 +// 11.6 --> 12 +// +// 11.5 --> 12 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Round/round3.fs b/snippets/fsharp/System/Math/Round/round3.fs new file mode 100644 index 00000000000..31bf59784ae --- /dev/null +++ b/snippets/fsharp/System/Math/Round/round3.fs @@ -0,0 +1,16 @@ +module round3 + +// +open System + +let values = [| 2.125; 2.135; 2.145; 3.125; 3.135; 3.145 |] +for value in values do + printfn $"{value} --> {Math.Round(value, 2)}" +// The example displays the following output: +// 2.125 --> 2.12 +// 2.135 --> 2.13 +// 2.145 --> 2.14 +// 3.125 --> 3.12 +// 3.135 --> 3.14 +// 3.145 --> 3.14 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Round/round4.fs b/snippets/fsharp/System/Math/Round/round4.fs new file mode 100644 index 00000000000..7e3fe5bd76a --- /dev/null +++ b/snippets/fsharp/System/Math/Round/round4.fs @@ -0,0 +1,16 @@ +module round4 + +// +open System + +let values = [| 2.125; 2.135; 2.145; 3.125; 3.135; 3.145 |] +for value in values do + printfn $"{value} --> {Math.Round(value, 2, MidpointRounding.AwayFromZero)}" +// The example displays the following output: +// 2.125 --> 2.13 +// 2.135 --> 2.13 +// 2.145 --> 2.15 +// 3.125 --> 3.13 +// 3.135 --> 3.14 +// 3.145 --> 3.15 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Round/round5.fs b/snippets/fsharp/System/Math/Round/round5.fs new file mode 100644 index 00000000000..22612ee6a05 --- /dev/null +++ b/snippets/fsharp/System/Math/Round/round5.fs @@ -0,0 +1,29 @@ +module round5 + +// +open System + +let roundValueAndAdd (value: float) = + printfn $"{value} --> {Math.Round(value, MidpointRounding.AwayFromZero)}" + value + 0.1 + +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: +// 11.1 --> 11 +// 11.2 --> 11 +// 11.3 --> 11 +// 11.4 --> 11 +// 11.5 --> 11 +// 11.6 --> 12 +// +// 11.5 --> 12 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Round/rounddecimal1.fs b/snippets/fsharp/System/Math/Round/rounddecimal1.fs new file mode 100644 index 00000000000..4623cbf7e4e --- /dev/null +++ b/snippets/fsharp/System/Math/Round/rounddecimal1.fs @@ -0,0 +1,16 @@ +module rounddecimal1 + +// +open System + +for value in 4.2m .. 0.1m .. 4.8m do + printfn $"{value} --> {Math.Round value}" +// The example displays the following output: +// 4.2 --> 4 +// 4.3 --> 4 +// 4.4 --> 4 +// 4.5 --> 4 +// 4.6 --> 5 +// 4.7 --> 5 +// 4.8 --> 5 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Round/source.fs b/snippets/fsharp/System/Math/Round/source.fs new file mode 100644 index 00000000000..289f89a1cf6 --- /dev/null +++ b/snippets/fsharp/System/Math/Round/source.fs @@ -0,0 +1,11 @@ +module source + +// +open System + +printfn "Classic Math.Round in F#" +printfn $"{Math.Round(4.4)}" // 4 +printfn $"{Math.Round(4.5)}" // 4 +printfn $"{Math.Round(4.6)}" // 5 +printfn $"{Math.Round(5.5)}" // 6 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Round/source1.fs b/snippets/fsharp/System/Math/Round/source1.fs new file mode 100644 index 00000000000..39e2ff6d773 --- /dev/null +++ b/snippets/fsharp/System/Math/Round/source1.fs @@ -0,0 +1,13 @@ +module source1 + +// +open System + +printfn $"{Math.Round(3.44, 1)}" //Returns 3.4. +printfn $"{Math.Round(3.45, 1)}" //Returns 3.4. +printfn $"{Math.Round(3.46, 1)}" //Returns 3.5. + +printfn $"{Math.Round(4.34, 1)}" // Returns 4.3 +printfn $"{Math.Round(4.35, 1)}" // Returns 4.4 +printfn $"{Math.Round(4.36, 1)}" // Returns 4.4 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Sign/fs.fsproj b/snippets/fsharp/System/Math/Sign/fs.fsproj new file mode 100644 index 00000000000..39f5fdd893e --- /dev/null +++ b/snippets/fsharp/System/Math/Sign/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Sign/sign.fs b/snippets/fsharp/System/Math/Sign/sign.fs new file mode 100644 index 00000000000..fb6495cba51 --- /dev/null +++ b/snippets/fsharp/System/Math/Sign/sign.fs @@ -0,0 +1,55 @@ +// +// This example demonstrates Math.Sign() +// In F#, the sign function may be used instead +open System + +let test = function + | 0 -> + "equal to" + | x when x < 0 -> + "less than" + | _ -> + "greater than" + +let print typ a b = + printfn $"{typ}: {a,3} is {b} zero." + +let xByte1 = 0uy +let xShort1 = -2s +let xInt1 = -3 +let xLong1 = -4L +let xSingle1 = 0f +let xDouble1 = 6. +let xDecimal1 = -7m +let xIntPtr1 = 8 + +// The following type is not CLS-compliant. +let xSbyte1 = -101y + +printfn "\nTest the sign of the following types of values:" +print "Byte " xByte1 (test (Math.Sign xByte1)) +print "Int16 " xShort1 (test (Math.Sign xShort1)) +print "Int32 " xInt1 (test (Math.Sign xInt1)) +print "Int64 " xLong1 (test (Math.Sign xLong1)) +print "Single " xSingle1 (test (Math.Sign xSingle1)) +print "Double " xDouble1 (test (Math.Sign xDouble1)) +print "Decimal" xDecimal1 (test (Math.Sign xDecimal1)) +print "IntPtr" xIntPtr1 (test (Math.Sign xIntPtr1)) + +printfn "\nThe following type is not CLS-compliant." +print "SByte " xSbyte1 (test (Math.Sign xSbyte1)) + +// This example produces the following results: +// Test the sign of the following types of values: +// Byte : 0 is equal to zero. +// Int16 : -2 is less than zero. +// Int32 : -3 is less than zero. +// Int64 : -4 is less than zero. +// Single : 0 is equal to zero. +// Double : 6 is greater than zero. +// Decimal: -7 is less than zero. +// IntPtr: 8 is greater than zero. +// +// The following type is not CLS-compliant. +// SByte : -101 is less than zero. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Sqrt/fs.fsproj b/snippets/fsharp/System/Math/Sqrt/fs.fsproj new file mode 100644 index 00000000000..38950a36fe5 --- /dev/null +++ b/snippets/fsharp/System/Math/Sqrt/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Sqrt/sqrt1.fs b/snippets/fsharp/System/Math/Sqrt/sqrt1.fs new file mode 100644 index 00000000000..8ae2bd00055 --- /dev/null +++ b/snippets/fsharp/System/Math/Sqrt/sqrt1.fs @@ -0,0 +1,27 @@ +// +open System + +// Create a list containing the area of some cities. +let areas = + [ "Sitka, Alaska", 2870.3 + "New York City", 302.6 + "Los Angeles", 468.7 + "Detroit", 138.8 + "Chicago", 227.1 + "San Diego", 325.2 ] + +printfn "%-18s %14s} %2s\n" "City" "Area (mi.)" "Equivalent to a square with:" + +for city, area in areas do + printfn $"{city,-18} {area,14:N1} {Math.Round(Math.Sqrt(area), 2),14:N2} miles per side" + +// The example displays the following output: +// City Area (mi.) Equivalent to a square with: +// +// Sitka, Alaska 2,870.3 53.58 miles per side +// New York City 302.6 17.40 miles per side +// Los Angeles 468.7 21.65 miles per side +// Detroit 138.8 11.78 miles per side +// Chicago 227.1 15.07 miles per side +// San Diego 325.2 18.03 miles per side +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Tanh/fs.fsproj b/snippets/fsharp/System/Math/Tanh/fs.fsproj new file mode 100644 index 00000000000..f6252f024b5 --- /dev/null +++ b/snippets/fsharp/System/Math/Tanh/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Tanh/tanh.fs b/snippets/fsharp/System/Math/Tanh/tanh.fs new file mode 100644 index 00000000000..dcabd235173 --- /dev/null +++ b/snippets/fsharp/System/Math/Tanh/tanh.fs @@ -0,0 +1,76 @@ +// +// Example for the hyperbolic Math.Tanh( double ) method. +// In F#, the tanh function may be used instead +open System + +// Evaluate hyperbolic identities with a given argument. +let useTanh arg = + let tanhArg = Math.Tanh arg + + // Evaluate tanh(X) = sinh(X) / cosh(X). + printfn $""" + Math.Tanh({arg}) = {tanhArg:E16} + Math.Sinh({arg}) / Math.Cosh({arg}) = {Math.Sinh arg / Math.Cosh arg:E16}""" + + // Evaluate tanh(2 * X) = 2 * tanh(X) / (1 + tanh^2(X)). + printfn $" 2 * Math.Tanh({arg}) / {2. * tanhArg}" + printfn $" (1 + (Math.Tanh({arg}))^2) = {2. * tanhArg / (1. + tanhArg * tanhArg):E16}" + printfn $" Math.Tanh({2. * arg}) = {Math.Tanh(2. * arg):E16}" + +// Evaluate a hyperbolic identity that is a function of two arguments. +let useTwoArgs argX argY = + // Evaluate tanh(X + Y) = (tanh(X) + tanh(Y)) / (1 + tanh(X) * tanh(Y)). + printfn $"\n (Math.Tanh({argX}) + Math.Tanh({argY})) /\n(1 + Math.Tanh({argX}) * Math.Tanh({argY})) = {(Math.Tanh argX + Math.Tanh argY) / (1. + Math.Tanh argX * Math.Tanh argY):E16}" + printfn $" Math.Tanh({argX + argY}) = {Math.Tanh(argX + argY):E16}" + +printfn "This example of hyperbolic Math.Tanh( double )\ngenerates the following output." +printfn "\nEvaluate these hyperbolic identities with selected values for X:" +printfn " tanh(X) = sinh(X) / cosh(X)" +printfn " tanh(2 * X) = 2 * tanh(X) / (1 + tanh^2(X))" + +useTanh 0.1 +useTanh 1.2 +useTanh 4.9 + +printfn "\nEvaluate [tanh(X + Y) = (tanh(X) + tanh(Y)) / (1 + tanh(X) * tanh(Y))]\nwith selected values for X and Y:" + +useTwoArgs 0.1 1.2 +useTwoArgs 1.2 4.9 + + +// This example of hyperbolic Math.Tanh( double ) +// generates the following output. +// +// Evaluate these hyperbolic identities with selected values for X: +// tanh(X) = sinh(X) / cosh(X) +// tanh(2 * X) = 2 * tanh(X) / (1 + tanh^2(X)) +// +// Math.Tanh(0.1) = 9.9667994624955819E-002 +// Math.Sinh(0.1) / Math.Cosh(0.1) = 9.9667994624955819E-002 +// 2 * Math.Tanh(0.1) / +// (1 + (Math.Tanh(0.1))^2) = 1.9737532022490401E-001 +// Math.Tanh(0.2) = 1.9737532022490401E-001 +// +// Math.Tanh(1.2) = 8.3365460701215521E-001 +// Math.Sinh(1.2) / Math.Cosh(1.2) = 8.3365460701215521E-001 +// 2 * Math.Tanh(1.2) / +// (1 + (Math.Tanh(1.2))^2) = 9.8367485769368024E-001 +// Math.Tanh(2.4) = 9.8367485769368024E-001 +// +// Math.Tanh(4.9) = 9.9988910295055444E-001 +// Math.Sinh(4.9) / Math.Cosh(4.9) = 9.9988910295055433E-001 +// 2 * Math.Tanh(4.9) / +// (1 + (Math.Tanh(4.9))^2) = 9.9999999385024030E-001 +// Math.Tanh(9.8) = 9.9999999385024030E-001 +// +// Evaluate [tanh(X + Y) = (tanh(X) + tanh(Y)) / (1 + tanh(X) * tanh(Y))] +// with selected values for X and Y: +// +// (Math.Tanh(0.1) + Math.Tanh(1.2)) / +// (1 + Math.Tanh(0.1) * Math.Tanh(1.2)) = 8.6172315931330645E-001 +// Math.Tanh(1.3) = 8.6172315931330634E-001 +// +// (Math.Tanh(1.2) + Math.Tanh(4.9)) / +// (1 + Math.Tanh(1.2) * Math.Tanh(4.9)) = 9.9998993913939649E-001 +// Math.Tanh(6.1) = 9.9998993913939649E-001 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Truncate/Truncate1.fs b/snippets/fsharp/System/Math/Truncate/Truncate1.fs new file mode 100644 index 00000000000..4aa5b0adfe1 --- /dev/null +++ b/snippets/fsharp/System/Math/Truncate/Truncate1.fs @@ -0,0 +1,25 @@ + +open System + +let main _ = + // + let floatNumber = 32.7865 + // Displays 32 + printfn $"{Math.Truncate floatNumber}" + // printfn $"{truncate floatNumber}" + + let floatNumber = -32.9012 + // Displays -32 + printfn $"{Math.Truncate floatNumber}" + // + + // + let decimalNumber = 32.7865m + // Displays 32 + printfn $"{Math.Truncate decimalNumber}" + + let decimalNumber = -32.9012m + // Displays -32 + printfn $"{Math.Truncate decimalNumber}" + // + 0 \ No newline at end of file diff --git a/snippets/fsharp/System/Math/Truncate/fs.fsproj b/snippets/fsharp/System/Math/Truncate/fs.fsproj new file mode 100644 index 00000000000..6450df66376 --- /dev/null +++ b/snippets/fsharp/System/Math/Truncate/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/xml/System/Math.xml b/xml/System/Math.xml index cf290f859c9..95e8c452716 100644 --- a/xml/System/Math.xml +++ b/xml/System/Math.xml @@ -56,6 +56,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/MathSample/CPP/mathsample.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Math/Overview/mathsample.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Overview/mathsample.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/MathSample/VB/mathsample.vb" id="Snippet1"::: ]]> @@ -136,6 +137,7 @@ The following example uses the method to get the absolute value of a number of values. :::code language="csharp" source="~/snippets/csharp/System/Math/Abs/Abs1.cs" interactive="try-dotnet-method" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Abs/Abs1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Abs/vb/Abs1.vb" id="Snippet1"::: ]]> @@ -206,6 +208,7 @@ The following example uses the method to get the absolute value of a number of values. :::code language="csharp" source="~/snippets/csharp/System/Math/Abs/abs2.cs" interactive="try-dotnet-method" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Abs/abs2.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Abs/vb/abs2.vb" id="Snippet2"::: ]]> @@ -274,6 +277,7 @@ The following example uses the method to get the absolute value of a number of values. :::code language="csharp" source="~/snippets/csharp/System/Math/Abs/abs3.cs" interactive="try-dotnet-method" id="Snippet3"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Abs/abs3.fs" id="Snippet3"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Abs/vb/abs3.vb" id="Snippet3"::: ]]> @@ -344,6 +348,7 @@ The following example uses the method to get the absolute value of a number of values. :::code language="csharp" source="~/snippets/csharp/System/Math/Abs/abs4.cs" interactive="try-dotnet-method" id="Snippet4"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Abs/abs4.fs" id="Snippet4"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Abs/vb/abs4.vb" id="Snippet4"::: ]]> @@ -414,6 +419,7 @@ The following example uses the method to get the absolute value of a number of values. :::code language="csharp" source="~/snippets/csharp/System/Math/Abs/abs5.cs" interactive="try-dotnet-method" id="Snippet5"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Abs/abs5.fs" id="Snippet5"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Abs/vb/abs5.vb" id="Snippet5"::: ]]> @@ -522,6 +528,7 @@ The following example uses the method to get the absolute value of a number of values. :::code language="csharp" source="~/snippets/csharp/System/Math/Abs/abs6.cs" interactive="try-dotnet-method" id="Snippet6"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Abs/abs6.fs" id="Snippet6"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Abs/vb/abs6.vb" id="Snippet6"::: ]]> @@ -594,6 +601,7 @@ The following example uses the method to get the absolute value of a number of values. :::code language="csharp" source="~/snippets/csharp/System/Math/Abs/abs7.cs" interactive="try-dotnet-method" id="Snippet7"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Abs/abs7.fs" id="Snippet7"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Abs/vb/abs7.vb" id="Snippet7"::: ]]> @@ -669,6 +677,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/MathSample/CPP/mathsample.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Math/Overview/mathsample.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Overview/mathsample.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/MathSample/VB/mathsample.vb" id="Snippet1"::: ]]> @@ -797,6 +806,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/MathSample/CPP/mathsample.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Math/Overview/mathsample.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Overview/mathsample.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/MathSample/VB/mathsample.vb" id="Snippet1"::: ]]> @@ -925,6 +935,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/math.atanx/CPP/atan.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Math/Atan/atan.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Atan/atan.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/math.atanx/VB/atan.vb" id="Snippet1"::: ]]> @@ -1020,6 +1031,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/math.atanx/CPP/atan.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Math/Atan/atan.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Atan/atan.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/math.atanx/VB/atan.vb" id="Snippet1"::: ]]> @@ -1134,6 +1146,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/math.bigmul/CPP/bigmul.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Math/BigMul/bigmul.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/BigMul/bigmul.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/math.bigmul/VB/bigmul.vb" id="Snippet1"::: ]]> @@ -1436,6 +1449,7 @@ The following example illustrates the method and contrasts it with the method. :::code language="csharp" source="~/snippets/csharp/System/Math/Ceiling/Ceiling1.cs" interactive="try-dotnet-method" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Ceiling/Ceiling1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Ceiling/vb/Ceiling1.vb" id="Snippet1"::: ]]> @@ -1514,6 +1528,7 @@ Dim i2 As Integer = CInt(Math.Ceiling(d2)) ' Result: 7969 The following example illustrates the method and contrasts it with the method. :::code language="csharp" source="~/snippets/csharp/System/Math/Ceiling/Ceiling1.cs" interactive="try-dotnet-method" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Ceiling/Ceiling1.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Ceiling/vb/Ceiling1.vb" id="Snippet2"::: ]]> @@ -2322,6 +2337,7 @@ Dim i2 As Integer = CInt(Math.Ceiling(d2)) ' Result: 7969 :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Math.SinCos/CPP/sincos.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Math/Cos/sincos.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Cos/sincos.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.SinCos/VB/sincos.vb" id="Snippet1"::: ]]> @@ -2393,6 +2409,7 @@ Dim i2 As Integer = CInt(Math.Ceiling(d2)) ' Result: 7969 :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Math.SinhCosh/CPP/sinhcosh.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Math/Cosh/sinhcosh.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Cosh/sinhcosh.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.SinhCosh/VB/sinhcosh.vb" id="Snippet1"::: ]]> @@ -2920,6 +2937,7 @@ Dim i2 As Integer = CInt(Math.Ceiling(d2)) ' Result: 7969 The following example demonstrates the method. :::code language="csharp" source="~/snippets/csharp/System/Math/DivRem/divrem1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/DivRem/divrem1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.divrem/vb/divrem1.vb" id="Snippet1"::: ]]> @@ -2990,6 +3008,7 @@ Dim i2 As Integer = CInt(Math.Ceiling(d2)) ' Result: 7969 The following example demonstrates the method. :::code language="csharp" source="~/snippets/csharp/System/Math/DivRem/divrem2.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/DivRem/divrem2.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.divrem/vb/divrem2.vb" id="Snippet2"::: ]]> @@ -3052,6 +3071,7 @@ Dim i2 As Integer = CInt(Math.Ceiling(d2)) ' Result: 7969 :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Math.E/CPP/efield.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Math/E/efield.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/E/efield.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.E/VB/efield.vb" id="Snippet1"::: ]]> @@ -3127,6 +3147,7 @@ Dim i2 As Integer = CInt(Math.Ceiling(d2)) ' Result: 7969 :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Math.Exp/CPP/exp.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Math/Exp/exp.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Exp/exp.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Exp/VB/exp.vb" id="Snippet1"::: ]]> @@ -3208,6 +3229,7 @@ Dim i2 As Integer = CInt(Math.Ceiling(d2)) ' Result: 7969 The following example illustrates the method and contrasts it with the method. :::code language="csharp" source="~/snippets/csharp/System/Math/Ceiling/Ceiling1.cs" interactive="try-dotnet-method" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Ceiling/Ceiling1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Ceiling/vb/Ceiling1.vb" id="Snippet1"::: ]]> @@ -3287,6 +3309,7 @@ Dim i2 As Integer = CInt(Math.Floor(d2)) ' Result: 7968 The following example illustrates the method and contrasts it with the method. :::code language="csharp" source="~/snippets/csharp/System/Math/Ceiling/Ceiling1.cs" interactive="try-dotnet-method" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Ceiling/Ceiling1.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Ceiling/vb/Ceiling1.vb" id="Snippet2"::: ]]> @@ -3427,6 +3450,7 @@ Remainder = (Math.Abs(dividend) - (Math.Abs(divisor) * The following example contrasts the remainder returned by the method with the remainder returned by the [remainder operator](/dotnet/csharp/language-reference/operators/remainder-operator). :::code language="csharp" source="~/snippets/csharp/System/Math/IEEERemainder/ieeeremainder1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/IEEERemainder/ieeeremainder1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.ieeeremainder/vb/ieeeremainder1.vb" id="Snippet1"::: ]]> @@ -3581,6 +3605,7 @@ Remainder = (Math.Abs(dividend) - (Math.Abs(divisor) * The following example illustrates the method. :::code language="csharp" source="~/snippets/csharp/System/Math/LogMethod/log1.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/LogMethod/log1.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Log_Overloads/VB/log1.vb" id="Snippet2"::: ]]> @@ -3736,6 +3761,7 @@ Remainder = (Math.Abs(dividend) - (Math.Abs(divisor) * :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Math.Log_Overloads/CPP/loggen.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Math/LogMethod/loggen.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/LogMethod/loggen.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Log_Overloads/VB/loggen.vb" id="Snippet1"::: ]]> @@ -3820,6 +3846,7 @@ Remainder = (Math.Abs(dividend) - (Math.Abs(divisor) * The following example uses the method to return the base 10 logarithm for selected values. :::code language="csharp" source="~/snippets/csharp/System/Math/Log10/log10.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Log10/log10.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Log10/VB/log10.vb" id="Snippet1"::: ]]> @@ -3905,6 +3932,7 @@ The following example demonstrates how to use the meth :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/math.max/CPP/max.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Math/Max/max.cs" interactive="try-dotnet-method" id="Snippet1"::: +:::code language="fsharp" source="~/snippets/fsharp/System/Math/Max/max.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/math.max/VB/max.vb" id="Snippet1"::: ]]> @@ -4712,6 +4740,7 @@ The following example demonstrates how to use the meth :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/math.min/CPP/min.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Math/Min/min.cs" interactive="try-dotnet-method" id="Snippet1"::: +:::code language="fsharp" source="~/snippets/fsharp/System/Math/Min/min.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/math.min/VB/min.vb" id="Snippet1"::: ]]> @@ -5553,6 +5582,7 @@ The following example demonstrates how to use the meth :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/MathSample/CPP/mathsample.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Math/Overview/mathsample.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Overview/mathsample.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/MathSample/VB/mathsample.vb" id="Snippet1"::: ]]> @@ -5645,6 +5675,7 @@ The following example demonstrates how to use the meth The following example uses the method to calculate the value that results from raising 2 to a power ranging from 0 to 32. :::code language="csharp" source="~/snippets/csharp/System/Math/Pow/pow1.cs" interactive="try-dotnet-method" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Pow/pow1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.pow/vb/pow1.vb" id="Snippet1"::: ]]> @@ -5806,6 +5837,7 @@ Rounding away from zero is the most widely known form of rounding, while roundin The following example illustrates the bias that can result from consistently rounding midpoint values in a single direction. The example computes the true mean of an array of values, and then computes the mean when the values in the array are rounded by using the two conventions. In this example, the true mean and the mean that results when rounding to nearest are the same. However, the mean that results when rounding away from zero differs by .05 (or by 3.6%) from the true mean. :::code language="csharp" source="~/snippets/csharp/System/Decimal/Round/mean1.cs" interactive="try-dotnet-method" id="Snippet2"::: +:::code language="fsharp" source="~/snippets/fsharp/System/Decimal/Round/mean1.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round.overload/vb/mean1.vb" id="Snippet2"::: By default, the method uses the round to nearest even convention. The following table lists the overloads of the method and the rounding convention that each uses. @@ -5829,6 +5861,7 @@ In order to determine whether a rounding operation involves a midpoint value, th The following example illustrates the problem. It repeatedly adds .1 to 11.0 and rounds the result to the nearest integer. 11.5 should round to 12 using either of the midpoint-rounding conventions (`ToEven` or `AwayFromZero`). However, as the output from the example shows, it does not. The example uses the "R" [standard numeric format string](/dotnet/standard/base-types/standard-numeric-format-strings) to display the floating point value's full precision, and shows that the value to be rounded has lost precision during repeated additions, and its value is actually 11.499999999999998. Because .499999999999998 is less than .5, the midpoint-rounding conventions don't come into play and the value is rounded down. As the example also shows, this problem does not occur if you assign the constant value 11.5 to a variable. :::code language="csharp" source="~/snippets/csharp/System/Decimal/Round/precision1.cs" interactive="try-dotnet" id="Snippet7"::: +:::code language="fsharp" source="~/snippets/fsharp/System/Decimal/Round/precision1.fs" id="Snippet7"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round.overload/vb/precision1.vb" id="Snippet7"::: Problems of precision in rounding midpoint values are most likely to arise in the following conditions: @@ -5846,6 +5879,7 @@ Problems of precision in rounding midpoint values are most likely to arise in th - Define a custom rounding algorithm that performs a "nearly equal" test to determine whether the value to be rounded is acceptably close to a midpoint value. The following example defines a `RoundApproximate` method that examines whether a fractional value is sufficiently near to a midpoint value to be subject to midpoint rounding. As the output from the example shows, it corrects the rounding problem shown in the previous example. :::code language="csharp" source="~/snippets/csharp/System/Decimal/Round/precision2.cs" interactive="try-dotnet" id="Snippet8"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Decimal/Round/precision2.fs" id="Snippet8"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round.overload/vb/precision2.vb" id="Snippet8"::: #### Rounding and single-precision floating-point values @@ -5853,6 +5887,7 @@ Problems of precision in rounding midpoint values are most likely to arise in th The method includes overloads that accept arguments of type and . There are no methods that round values of type . If you pass a value to one of the overloads of the method, it is cast (in C#) or converted (in Visual Basic) to a , and the corresponding overload with a parameter is called. Although this is a widening conversion, it often involves a loss of precision, as the following example illustrates. When a value of 16.325 is passed to the method and rounded to two decimal places using the rounding to nearest convention, the result is 16.33 and not the expected result of 16.32. :::code language="csharp" source="~/snippets/csharp/System/Decimal/Round/single1.cs" interactive="try-dotnet" id="Snippet1"::: +:::code language="fsharp" source="~/snippets/fsharp/System/Decimal/Round/single1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round.overload/vb/single1.vb" id="Snippet1"::: This unexpected result is due to a loss of precision in the conversion of the value to a . Because the resulting value of 16.325000762939453 is not a midpoint value and is greater than 16.325, it is always rounded upward. @@ -5940,6 +5975,7 @@ This method uses the default rounding convention of method. The value of 4.5 rounds to 4 rather than 5, because this overload uses the default convention. :::code language="csharp" source="~/snippets/csharp/System/Math/Round/rounddecimal1.cs" interactive="try-dotnet-method" id="Snippet6"::: +:::code language="fsharp" source="~/snippets/fsharp/System/Math/Round/rounddecimal1.fs" id="Snippet6"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round/vb/rounddecimal1.vb" id="Snippet6"::: ]]> @@ -6028,6 +6064,7 @@ The following example demonstrates rounding to the nearest integer value. :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_Classic/classic Math.Round Example/CPP/source.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Math/Round/source.cs" interactive="try-dotnet-method" id="Snippet1"::: +:::code language="fsharp" source="~/snippets/fsharp/System/Math/Round/source.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_Classic/classic Math.Round Example/VB/source.vb" id="Snippet1"::: ]]> @@ -6036,6 +6073,7 @@ The following example demonstrates rounding to the nearest integer value. Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operations on floating-point values, in some cases the method may not appear to round midpoint values to the nearest even integer. In the following example, because the floating-point value .1 has no finite binary representation, the first call to the method with a value of 11.5 returns 11 instead of 12. :::code language="csharp" source="~/snippets/csharp/System/Math/Round/round2.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Round/round2.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round/vb/round2.vb" id="Snippet1"::: @@ -6108,6 +6146,7 @@ This method uses the default rounding convention of @@ -6186,6 +6225,7 @@ For information about rounding numbers with midpoint values, see [Midpoint value The following example displays values returned by the method with different `mode` values. :::code language="csharp" source="~/snippets/csharp/System/Decimal/Round/midpoint1.cs" interactive="try-dotnet" id="Snippet5"::: +:::code language="fsharp" source="~/snippets/fsharp/System/Decimal/Round/midpoint1.fs" id="Snippet5"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round.overload/vb/midpoint1.vb" id="Snippet5"::: ]]> @@ -6264,6 +6304,7 @@ The following example rounds double values with two fractional digits to doubles :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_Classic/classic Math.Round2 Example/CPP/source.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Math/Round/source1.cs" id="Snippet1"::: +:::code language="fsharp" source="~/snippets/fsharp/System/Math/Round/source1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_Classic/classic Math.Round2 Example/VB/source.vb" id="Snippet1"::: ]]> @@ -6274,6 +6315,7 @@ The following example rounds double values with two fractional digits to doubles Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operations on floating-point values, in some cases the method may not appear to round midpoint values to the nearest even value in the decimal position. This is illustrated in the following example, where 2.135 is rounded to 2.13 instead of 2.14. This occurs because internally the method multiplies by 10digits, and the multiplication operation in this case suffers from a loss of precision. :::code language="csharp" source="~/snippets/csharp/System/Math/Round/round3.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Round/round3.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round/vb/round3.vb" id="Snippet2"::: @@ -6345,6 +6387,7 @@ If the value of the `value` argument is method with different `mode` values. :::code language="csharp" source="~/snippets/csharp/System/Decimal/Round/midpoint2.cs" interactive="try-dotnet" id="Snippet6"::: +:::code language="fsharp" source="~/snippets/fsharp/System/Decimal/Round/midpoint2.fs" id="Snippet6"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round.overload/vb/midpoint2.vb" id="Snippet6"::: ]]> @@ -6355,6 +6398,7 @@ The following example displays values returned by the Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operations on floating-point values, in some cases the method may not appear to round midpoint values to the nearest even integer. In the following example, because the floating-point value .1 has no finite binary representation, the first call to the method with a value of 11.5 returns 11 instead of 12. :::code language="csharp" source="~/snippets/csharp/System/Math/Round/round5.cs" interactive="try-dotnet" id="Snippet4"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Round/round5.fs" id="Snippet4"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round/vb/round5.vb" id="Snippet4"::: @@ -6434,6 +6478,7 @@ The value of the `decimals` argument can range from 0 to 28. The following example demonstrates how to use the method with the enumeration. :::code language="csharp" source="~/snippets/csharp/System/Decimal/Round/mpr.cs" interactive="try-dotnet-method" id="Snippet1"::: +:::code language="fsharp" source="~/snippets/fsharp/System/Decimal/Round/mpr.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round.overload/vb/mpr.vb" id="Snippet1"::: ]]> @@ -6514,6 +6559,7 @@ If the value of the `value` argument is method with the enumeration. :::code language="csharp" source="~/snippets/csharp/System/Decimal/Round/mpr.cs" interactive="try-dotnet-method" id="Snippet4"::: +:::code language="fsharp" source="~/snippets/fsharp/System/Decimal/Round/mpr.fs" id="Snippet4"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round.overload/vb/mpr.vb" id="Snippet4"::: ]]> @@ -6526,6 +6572,7 @@ The following example demonstrates how to use the Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operations on floating-point values, in some cases the method may not appear to round midpoint values as specified by the parameter. This is illustrated in the following example, where 2.135 is rounded to 2.13 instead of 2.14. This occurs because internally the method multiplies by 10digits, and the multiplication operation in this case suffers from a loss of precision. :::code language="csharp" source="~/snippets/csharp/System/Math/Round/round4.cs" interactive="try-dotnet-method" id="Snippet3"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Round/round4.fs" id="Snippet3"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round/vb/round4.vb" id="Snippet3"::: @@ -6669,6 +6716,7 @@ The following example demonstrates how to use the @@ -6751,6 +6799,7 @@ The following example demonstrates how to use the @@ -6835,6 +6884,7 @@ The following example demonstrates how to use the @@ -6917,6 +6967,7 @@ The following example demonstrates how to use the @@ -6999,6 +7050,7 @@ The following example demonstrates how to use the @@ -7061,6 +7113,7 @@ The following example demonstrates how to use the method to determine the sign of an value and display it to the console. :::code language="csharp" source="~/snippets/csharp/System/Math/Sign/sign.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Sign/sign.fs" id="Snippet1"::: ]]> @@ -7146,6 +7199,7 @@ The following example demonstrates how to use the @@ -7228,6 +7282,7 @@ The following example demonstrates how to use the @@ -7301,6 +7356,7 @@ The following example demonstrates how to use the @@ -7358,6 +7414,7 @@ The following example demonstrates how to use the to evaluate certain trigonometric identities for selected angles. :::code language="csharp" source="~/snippets/csharp/System/Math/Cos/sincos.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Cos/sincos.fs" id="Snippet1"::: ]]> @@ -7428,6 +7485,7 @@ The following example demonstrates how to use the @@ -7508,6 +7566,7 @@ The following example demonstrates how to use the @@ -7580,6 +7639,7 @@ The following example demonstrates how to use the @@ -7651,6 +7711,7 @@ The following example demonstrates how to use the @@ -7762,6 +7823,7 @@ The following example demonstrates how to use the method to truncate both a positive and a negative value. :::code language="csharp" source="~/snippets/csharp/System/Math/Truncate/Truncate1.cs" interactive="try-dotnet-method" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Truncate/Truncate1.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Truncate/vb/Truncate1.vb" id="Snippet2"::: ]]> @@ -7836,6 +7898,7 @@ Dim i As Integer = CInt(Math.Truncate(d)) ' Result: 164 The following example calls the method to truncate both a positive and a negative value. :::code language="csharp" source="~/snippets/csharp/System/Math/Truncate/Truncate1.cs" interactive="try-dotnet-method" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Math/Truncate/Truncate1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Math.Truncate/vb/Truncate1.vb" id="Snippet1"::: ]]>