Skip to content

Commit

Permalink
floating benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLloyd committed Nov 27, 2023
1 parent 95a7843 commit 6b493de
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CsCheck/CsCheck.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Increased Faster % to two decimal places.
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.110" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.112" PrivateAssets="All" />
<None Include="../CsCheck.png" Pack="true" PackagePath="" Visible="False" />
<None Include="../README.md" Pack="true" PackagePath="" Visible="False" />
</ItemGroup>
Expand Down
50 changes: 45 additions & 5 deletions Tests/FloatingPointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,66 @@ private static void DoubleSumPrecision(int significantFigures, int maxLength)
});
}

private static void DoubleKSumPrecision(int significantFigures, int maxLength)
{
const double scaling = 0.01;
var lower = (long)Math.Pow(10, significantFigures - 1);
var upper = (long)Math.Pow(10, significantFigures) - 1;
Gen.Long[lower, upper].Array[100, maxLength]
.Sample(longs =>
{
var longSum = longs.Sum();
var doubleSum = longs.Select(i => i * scaling).ToArray().KSum();
return (doubleSum / scaling).ToString("#") == longSum.ToString();
});
}

[Fact]
public void DoubleSumPrecision12()
{
DoubleSumPrecision(12, 350); // 9_999_999_999.99
DoubleSumPrecision(12, 350); // 1_000_000_000.00 - 9_999_999_999.99
}

[Fact]
public void DoubleKSumPrecision12()
{
DoubleKSumPrecision(12, 1_700); // 1_000_000_000.00 - 9_999_999_999.99
}

[Fact]
public void DoubleSumPrecision11()
{
DoubleSumPrecision(11, 1_500); // 999_999_999.99
DoubleSumPrecision(11, 1_500); // 100_000_000.00 - 999_999_999.99
}

[Fact]
public void DoubleKSumPrecision11()
{
DoubleKSumPrecision(11, 17_900); // 100_000_000.00 - 999_999_999.99
}

[Fact]
public void DoubleSumPrecision10()
{
DoubleSumPrecision(10, 7_100); // 99_999_999.99
DoubleSumPrecision(10, 7_100); // 10_000_000.00 - 99_999_999.99
}

[Fact]
public void DoubleKSumPrecision10()
{
DoubleKSumPrecision(10, 181_000); // 10_000_000.00 - 99_999_999.99
}

[Fact]
public void DoubleSumPrecision9()
{
DoubleSumPrecision(9, 35_500); // 9_999_999.99
DoubleSumPrecision(9, 35_500); // 1_000_000.00 - 9_999_999.99
}

[Fact]
public void DoubleKSumPrecision9()
{
DoubleKSumPrecision(9, 1_817_000); // 1_000_000.00 - 9_999_999.99
}

[Fact]
Expand Down Expand Up @@ -164,4 +202,6 @@ public readonly void Invoke()
// 2. Performance
// 3. Implementation
// 4. Scaling
// 5. Allocation and FSum
// 10_000_000_000 x 0.01 USD
// Realise on no primitive obsession and well defined io and service boundary.
// 5. Allocation and KSum
4 changes: 2 additions & 2 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" PrivateAssets="All" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4" PrivateAssets="All" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.10" />
<PackageReference Include="Meziantou.Xunit.ParallelTestFramework" Version="2.1.0" />
<PackageReference Include="Rationals" Version="2.3.0" />
Expand Down

0 comments on commit 6b493de

Please sign in to comment.