Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.7" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace FloatingPointEquality.Benchmark;

[InProcess]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
public class FloatingPointEqualityBenchmarks
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
Console.WriteLine($"{calculation == double.BitIncrement(expected)}");
Console.WriteLine($"{double.BitDecrement(calculation) == expected}");

var calculationBits = BitConverter.DoubleToInt64Bits(calculation);
var expectedBits = BitConverter.DoubleToInt64Bits(expected);
Console.WriteLine($"{long.Abs(calculationBits - expectedBits)}");

Console.WriteLine(0.0 == -0.0);

Console.WriteLine(double.Epsilon);
Console.WriteLine(double.Abs(calculation - expected) < double.Epsilon);
Console.WriteLine(1.0 + double.Epsilon == 1.0);

Console.WriteLine("\n----------------------------------------------------------\n");

Console.WriteLine($"{0.29999999999999999:G17}");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.Numerics;
using System.Runtime.CompilerServices;

namespace FloatingPointEquality.Library;

public static class FloatingPointComparisons
{
private static readonly double[] HalfNegativePowersOf10 =
{
[
1e0 * 0.5,
1e-1 * 0.5,
1e-2 * 0.5,
Expand All @@ -25,7 +24,7 @@ public static class FloatingPointComparisons
1e-15 * 0.5,
1e-16 * 0.5,
1e-17 * 0.5
};
];

public static bool EqualityUsingPrecision(double a, double b, int decimalPlaces)
{
Expand Down Expand Up @@ -57,9 +56,10 @@ public static bool EqualityUsingMaxUnitsInLastPlace(double a, double b, long max
if (maxUnitsInLastPlace < 1)
throw new ArgumentOutOfRangeException(nameof(maxUnitsInLastPlace));

if (double.IsNaN(a) || double.IsNaN(b)) return false;
if (double.IsNaN(a) || double.IsNaN(b))
return false;

if (double.IsInfinity(a) || double.IsInfinity(b) || a.IsNegative() != b.IsNegative())
if (double.IsInfinity(a) || double.IsInfinity(b) || double.IsNegative(a) != double.IsNegative(b))
return a == b;

return long.Abs(BitConverter.DoubleToInt64Bits(a) - BitConverter.DoubleToInt64Bits(b)) <= maxUnitsInLastPlace;
Expand All @@ -73,7 +73,7 @@ public static bool EqualityUsingMaxUnitsInLastPlace(float a, float b, int maxUni
if (float.IsNaN(a) || float.IsNaN(b))
return false;

if (float.IsInfinity(a) || float.IsInfinity(b) || a.IsNegative() != b.IsNegative())
if (float.IsInfinity(a) || float.IsInfinity(b) || float.IsNegative(a) != float.IsNegative(b))
return a == b;

return int.Abs(BitConverter.SingleToInt32Bits(a) - BitConverter.SingleToInt32Bits(b)) <= maxUnitsInLastPlace;
Expand All @@ -87,13 +87,9 @@ public static bool EqualityUsingMaxUnitsInLastPlace(Half a, Half b, int maxUnits
if (Half.IsNaN(a) || Half.IsNaN(b))
return false;

if (Half.IsInfinity(a) || Half.IsInfinity(b) || a.IsNegative() != b.IsNegative())
if (Half.IsInfinity(a) || Half.IsInfinity(b) || Half.IsNegative(a) != Half.IsNegative(b))
return a == b;

return int.Abs(BitConverter.HalfToInt16Bits(a) - BitConverter.HalfToInt16Bits(b)) <= maxUnitsInLastPlace;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsNegative<T>(this T value) where T : IFloatingPointIeee754<T>
=> T.IsNegative(value);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public void
const double a = 1.12345;
const double b = 1.12344;

var act1 = () => FloatingPointComparisons.EqualityUsingPrecision(a, b, 20);
var act2 = () => FloatingPointComparisons.EqualityUsingPrecision(a, b, -1);
Action act1 = () => FloatingPointComparisons.EqualityUsingPrecision(a, b, 20);
Action act2 = () => FloatingPointComparisons.EqualityUsingPrecision(a, b, -1);

act1.Should().Throw<IndexOutOfRangeException>();
act2.Should().Throw<IndexOutOfRangeException>();
Assert.Throws<IndexOutOfRangeException>(act1);
Assert.Throws<IndexOutOfRangeException>(act2);
}

[Fact]
Expand All @@ -25,7 +25,7 @@ public void GivenTwoDoubleNaNs_WhenEqualityComparisonWithPrecisionIsCalled_ThenS

var result = FloatingPointComparisons.EqualityUsingPrecision(a, b, decimalPlaces);

result.Should().BeFalse();
Assert.False(result);
}

[Fact]
Expand All @@ -37,7 +37,7 @@ public void GivenTwoDoubleInfinityValues_WhenEqualityComparisonWithPrecisionIsCa

var result = FloatingPointComparisons.EqualityUsingPrecision(a, b, decimalPlaces);

result.Should().BeTrue();
Assert.True(result);
}

[Fact]
Expand All @@ -50,7 +50,7 @@ public void

var result = FloatingPointComparisons.EqualityUsingPrecision(a, b, decimalPlaces);

result.Should().BeFalse();
Assert.False(result);
}

[Fact]
Expand All @@ -63,7 +63,7 @@ public void

var result = FloatingPointComparisons.EqualityUsingPrecision(a, b, decimalPlaces);

result.Should().BeTrue();
Assert.True(result);
}

[Fact]
Expand All @@ -76,7 +76,7 @@ public void

var result = FloatingPointComparisons.EqualityUsingPrecision(a, b, decimalPlaces);

result.Should().BeFalse();
Assert.False(result);
}

[Fact]
Expand All @@ -88,7 +88,7 @@ public void GivenTwoDifferentDoubles_WhenEqualityComparisonWithPrecisionIsCalled

var result = FloatingPointComparisons.EqualityUsingPrecision(a, b, decimalPlaces);

result.Should().BeFalse();
Assert.False(result);
}

[Fact]
Expand All @@ -100,7 +100,7 @@ public void GivenTwoDoubleNaNs_WhenEqualityComparisonWithToleranceIsCalled_ThenS

var result = FloatingPointComparisons.EqualityUsingTolerance(a, b, tolerance);

result.Should().BeFalse();
Assert.False(result);
}

[Fact]
Expand All @@ -112,7 +112,7 @@ public void GivenTwoDoubleInfinityValues_WhenEqualityComparisonWithToleranceIsCa

var result = FloatingPointComparisons.EqualityUsingTolerance(a, b, tolerance);

result.Should().BeTrue();
Assert.True(result);
}

[Fact]
Expand All @@ -125,7 +125,7 @@ public void

var result = FloatingPointComparisons.EqualityUsingTolerance(a, b, tolerance);

result.Should().BeFalse();
Assert.False(result);
}

[Fact]
Expand All @@ -138,7 +138,7 @@ public void

var result = FloatingPointComparisons.EqualityUsingTolerance(a, b, tolerance);

result.Should().BeTrue();
Assert.True(result);
}

[Fact]
Expand All @@ -151,7 +151,7 @@ public void

var result = FloatingPointComparisons.EqualityUsingTolerance(a, b, tolerance);

result.Should().BeFalse();
Assert.False(result);
}

[Fact]
Expand All @@ -163,7 +163,7 @@ public void GivenTwoDifferentDoubles_WhenEqualityComparisonWithToleranceIsCalled

var result = FloatingPointComparisons.EqualityUsingTolerance(a, b, tolerance);

result.Should().BeFalse();
Assert.False(result);
}

[Fact]
Expand All @@ -175,7 +175,7 @@ public void GivenTwoDoubleNaNs_WhenEqualityComparisonUsingMaxUlpIsCalled_ThenSho

var result = FloatingPointComparisons.EqualityUsingMaxUnitsInLastPlace(a, b, maxUlp);

result.Should().BeFalse();
Assert.False(result);
}

[Fact]
Expand All @@ -187,7 +187,7 @@ public void GivenTwoDoubleInfinityValues_WhenEqualityComparisonUsingMaxUlpIsCall

var result = FloatingPointComparisons.EqualityUsingMaxUnitsInLastPlace(a, b, maxUlp);

result.Should().BeTrue();
Assert.True(result);
}

[Fact]
Expand All @@ -200,7 +200,7 @@ public void

var result = FloatingPointComparisons.EqualityUsingMaxUnitsInLastPlace(a, b, maxUlp);

result.Should().BeFalse();
Assert.False(result);
}

[Fact]
Expand All @@ -213,7 +213,7 @@ public void

var result = FloatingPointComparisons.EqualityUsingMaxUnitsInLastPlace(a, b, maxUlp);

result.Should().BeTrue();
Assert.True(result);
}

[Fact]
Expand All @@ -226,7 +226,7 @@ public void

var result = FloatingPointComparisons.EqualityUsingMaxUnitsInLastPlace(a, b, maxUlp);

result.Should().BeFalse();
Assert.False(result);
}

[Fact]
Expand All @@ -237,8 +237,8 @@ public void
const double b = 1.1234500000000012;
const long maxUlp = 0;

var act = () => FloatingPointComparisons.EqualityUsingMaxUnitsInLastPlace(a, b, maxUlp);
act.Should().Throw<ArgumentOutOfRangeException>();
Action act = () => FloatingPointComparisons.EqualityUsingMaxUnitsInLastPlace(a, b, maxUlp);
Assert.Throws<ArgumentOutOfRangeException>(act);
}

[Fact]
Expand All @@ -249,7 +249,7 @@ public void
const double b = 1.1234500000000012;
const long maxUlp = -1;

var act = () => FloatingPointComparisons.EqualityUsingMaxUnitsInLastPlace(a, b, maxUlp);
act.Should().Throw<ArgumentOutOfRangeException>();
Action act = () => FloatingPointComparisons.EqualityUsingMaxUnitsInLastPlace(a, b, maxUlp);
Assert.Throws<ArgumentOutOfRangeException>(act);
}
}
Loading
Loading