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

Skip to content

System.Single F# snippets #7850

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Double/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="precisionlist1.fs" />
</ItemGroup>
</Project>
8 changes: 8 additions & 0 deletions snippets/fsharp/System/Double/Overview/precisionlist1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// <Snippet5>
let value1 = 1. / 3.
let sValue2 = 1f / 3f
let value2 = double sValue2
printfn $"{value1:R} = {value2:R}: {value1.Equals value2}"
// The example displays the following output:
// 0.33333333333333331 = 0.3333333432674408: False
// </Snippet5>
13 changes: 13 additions & 0 deletions snippets/fsharp/System/Single/CompareTo/compareto2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module compareto2

// <Snippet1>
let value1 = 16.5457f
let operand = 3.8899982f
let value2 = value1 * operand / operand
printfn $"Comparing {value1} and {value2}: {value1.CompareTo value2}\n"
printfn $"Comparing {value1:R} and {value2:R}: {value1.CompareTo value2}"
// The example displays the following output:
// Comparing 16.5457 and 16.5457: -1
//
// Comparing 16.5457 and 16.545702: -1
// </Snippet1>
13 changes: 13 additions & 0 deletions snippets/fsharp/System/Single/CompareTo/compareto3.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module compareto3

// <Snippet2>
let value1 = 16.5457f
let operand = 3.8899982f
let value2 = box (value1 * operand / operand)
printfn $"Comparing {value1} and {value2}: {value1.CompareTo value2}\n"
printfn $"Comparing {value1:R} and {value2:R}: {value1.CompareTo value2}"
// The example displays the following output:
// Comparing 16.5457 and 16.5457: -1
//
// Comparing 16.5457 and 16.545702: -1
// </Snippet2>
12 changes: 12 additions & 0 deletions snippets/fsharp/System/Single/CompareTo/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="compareto3.fs" />
<Compile Include="compareto2.fs" />
<Compile Include="singlesample.fs" />
</ItemGroup>
</Project>
119 changes: 119 additions & 0 deletions snippets/fsharp/System/Single/CompareTo/singlesample.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
module singlesample

open System

[<EntryPoint>]
let main _ =
//<snippet1>
let s = 4.55f
//</snippet1>

//<snippet2>
printfn $"A Single is of type {s.GetType()}."
//</snippet2>

//<snippet3>
let mutable finished = false
while not finished do
printf "Enter a real number: "
let inp = stdin.ReadLine()
try
let s = Single.Parse inp
printfn $"You entered {s}."
finished <- true
with
| :? FormatException ->
printfn "You did not enter a number."
| e ->
printfn "An exception occurred while parsing your response: {e}"
//</snippet3>

//<snippet4>
if s > Single.MaxValue then
printfn "Your number is larger than a Single."
//</snippet4>

//<snippet5>
if s < Single.MinValue then
printfn "Your number is smaller than a Single."
//</snippet5>

//<snippet6>
printfn $"Epsilon, or the permittivity of a vacuum, has value {Single.Epsilon}"
//</snippet6>

//<snippet7>
let zero = 0f

// This condition will return false.
if 0f / zero = Single.NaN then
printfn "0 / 0 can be tested with Single.NaN."
else
printfn "0 / 0 cannot be tested with Single.NaN use Single.IsNan() instead."
//</snippet7>

//<snippet8>
// This will return true.
if Single.IsNaN(0f / zero) then
printfn "Single.IsNan() can determine whether a value is not-a-number."
//</snippet8>

//<snippet9>
// This will equal Infinity.
printfn $"10.0 minus NegativeInfinity equals {10f - Single.NegativeInfinity}."
//</snippet9>

//<snippet10>
// This will equal Infinity.
printfn $"PositiveInfinity plus 10.0 equals {Single.PositiveInfinity + 10f}."
//</snippet10>

//<snippet11>
// This will return "true".
printfn $"IsInfinity(3.0F / 0) == %b{Single.IsInfinity(3f / 0f)}."
//</snippet11>

//<snippet12>
// This will return true.
printfn $"IsPositiveInfinity(4.0F / 0) == {Single.IsPositiveInfinity(4f / 0f)}."
//</snippet12>

//<snippet13>
// This will return true.
printfn $"IsNegativeInfinity(-5.0F / 0) == {Single.IsNegativeInfinity(-5f / 0f)}."
//</snippet13>

//<snippet14>
let a = 500f
//</snippet14>

//<snippet15>
// The variables point to the same objects.
let mutable obj1: obj = a
let obj2: obj = obj1

if Single.ReferenceEquals(obj1, obj2) then
printfn "The variables point to the same Single object."
else
printfn "The variables point to different Single objects."
//</snippet15>

//<snippet16>
let obj1 = single 450

if a.CompareTo obj1 < 0 then
printfn $"{a} is less than {obj1}."

if a.CompareTo obj1 > 0 then
printfn $"{a} is greater than {obj1}."

if a.CompareTo obj1 = 0 then
printfn $"{a} equals {obj1}."
//</snippet16>

//<snippet17>
let obj1 = single 500
if a.Equals obj1 then
printfn "The value type and reference type values are equal."
//</snippet17>
0
62 changes: 62 additions & 0 deletions snippets/fsharp/System/Single/Epsilon/SingleEquals_25051.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module SingleEquals_25021

open System

let compareUsingEquals () =
// <Snippet1>
// Initialize two floats with apparently identical values
let float1 = 0.33333f
let float2 = 1f / 3f
// Compare them for equality
printfn $"{float1.Equals float2}" // displays false
// </Snippet1>

let compareApproximateValues () =
// <Snippet2>
// Initialize two floats with apparently identical values
let float1 = 0.33333f
let float2 = 1f / 3f
// Define the tolerance for variation in their values
let difference = abs (float1 * 0.0001f)

// Compare the values
// The output to the console indicates that the two values are equal
if abs (float1 - float2) <= difference then
printfn "float1 and float2 are equal."
else
printfn "float1 and float2 are unequal."
// </Snippet2>

let compareObjectsUsingEquals () =
// <Snippet3>
// Initialize two floats with apparently identical values
let float1 = 0.33333f
let float2 = box (1f / 3f)
// Compare them for equality
printfn $"{float1.Equals float2}" // displays false
// </Snippet3>

let compareApproximateObjectValues () =
// <Snippet4>
// Initialize two floats with apparently identical values
let float1 = 0.33333f
let float2 = box (1f / 3f)
// Define the tolerance for variation in their values
let difference = abs (float1 * 0.0001f)

// Compare the values
// The output to the console indicates that the two values are equal
if abs (float1 - (float2 :?> float32)) <= difference then
printfn "float1 and float2 are equal."
else
printfn "float1 and float2 are unequal."
// </Snippet4>

compareUsingEquals ()
printfn ""
compareApproximateValues ()
printfn ""
compareObjectsUsingEquals ()
printfn ""
compareApproximateObjectValues ()
printfn ""
17 changes: 17 additions & 0 deletions snippets/fsharp/System/Single/Epsilon/epsilon.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module epsilon

// <Snippet5>
open System

let values = [ 0f; Single.Epsilon; Single.Epsilon * 0.5f ]

for i = 0 to values.Length - 2 do
for i2 = i + 1 to values.Length - 1 do
printfn $"{values[i]:r} = {values[i2]:r}: {values[i].Equals(values[i2])}"
printfn ""
// The example displays the following output:
// 0 = 1.401298E-45: False
// 0 = 0: True
//
// 1.401298E-45 = 0: False
// </Snippet5>
44 changes: 44 additions & 0 deletions snippets/fsharp/System/Single/Epsilon/epsilon1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module epsilon1

// <Snippet6>
open System

let getComponentParts (value: float32) =
let result = $"{value:R}: "
let indent = result.Length

// Convert the single to a 4-byte array.
let bytes = BitConverter.GetBytes value
let formattedSingle = BitConverter.ToInt32(bytes, 0)

// Get the sign bit (byte 3, bit 7).
let result = result + $"""Sign: {if formattedSingle >>> 31 <> 0 then "1 (-)" else "0 (+)"}\n"""

// Get the exponent (byte 2 bit 7 to byte 3, bits 6)
let exponent = (formattedSingle >>> 23) &&& 0x000000FF
let adjustment = if exponent <> 0 then 127 else 126
let result = result + $"{String(' ', indent)}Exponent: 0x{1:X4} ({exponent - adjustment})\n"

// Get the significand (bits 0-22)
let significand =
if exponent <> 0 then
(formattedSingle &&& 0x007FFFFF) ||| 0x800000
else
formattedSingle &&& 0x007FFFFF

result + $"{String(' ', indent)}Mantissa: 0x{significand:X13}\n"


let values = [ 0f; Single.Epsilon ]
for value in values do
printfn $"{getComponentParts value}\n"
// // The example displays the following output:
// 0: Sign: 0 (+)
// Exponent: 0xFFFFFF82 (-126)
// Mantissa: 0x0000000000000
//
//
// 1.401298E-45: Sign: 0 (+)
// Exponent: 0xFFFFFF82 (-126)
// Mantissa: 0x0000000000001
// </Snippet6>
12 changes: 12 additions & 0 deletions snippets/fsharp/System/Single/Epsilon/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="epsilon.fs" />
<Compile Include="epsilon1.fs" />
<Compile Include="SingleEquals_25051.fs" />
</ItemGroup>
</Project>
27 changes: 27 additions & 0 deletions snippets/fsharp/System/Single/Equals/equalsabs1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module equalsabs1

// <Snippet1>
open System

let hasMinimalDifference (value1: float32) (value2: float32) units =
let bytes = BitConverter.GetBytes value1
let iValue1 = BitConverter.ToInt32(bytes, 0)
let bytes = BitConverter.GetBytes(value2)
let iValue2 = BitConverter.ToInt32(bytes, 0)

// If the signs are different, return false except for +0 and -0.
if (iValue1 >>> 31) <> (iValue2 >>> 31) then
value1 = value2
else
let diff = abs (iValue1 - iValue2)
diff <= units

let value1 = 0.1f * 10f
let value2 =
List.replicate 10 0.1f
|> List.sum

printfn $"{value1:R} = {value2:R}: {hasMinimalDifference value1 value2 1}"
// The example displays the following output:
// 1 = 1.0000001: True
// </Snippet1>
Loading