Skip to content

Commit

Permalink
Improved output distribution of RoundToLevels (#25026)
Browse files Browse the repository at this point in the history
* Improved output distribution of RoundToLevels

* Updated tests

* Oops

(cherry picked from commit 9cee54e5a1b4efd1b8d511584b3e4dcbd7e7c529)
  • Loading branch information
Tayrtahn authored and DebugOk committed Feb 8, 2024
1 parent aac9564 commit de74e0f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 27 deletions.
22 changes: 1 addition & 21 deletions Content.Shared/Rounding/ContentHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,7 @@ public static int RoundToLevels(double actual, double max, int levels)
}

var toOne = actual / max;
double threshold;
if (levels % 2 == 0)
{
// Basically, if we have an even count of levels, there's no exact "mid point".
// Thus, I nominate the first one below the 50% mark.
threshold = ((levels / 2f) - 1) / (levels - 1);
}
else
{
threshold = 0.5f;
}

var preround = toOne * (levels - 1);
if (toOne < threshold || levels <= 2)
{
return (int) Math.Ceiling(preround);
}
else
{
return (int) Math.Floor(preround);
}
return (int) Math.Ceiling(toOne * (levels - 2));
}

/// <summary>
Expand Down
12 changes: 6 additions & 6 deletions Content.Tests/Shared/Utility/ContentHelpers_Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public sealed class ContentHelpers_Test
(0.01f, 10, 5, 1),
(1, 10, 5, 1),
(2, 10, 5, 1),
(2.5f, 10, 5, 1),
(2.51f, 10, 5, 2),
(3, 10, 5, 2),
(3, 10, 5, 1),
(3.33f, 10, 5, 1),
(3.34f, 10, 5, 2),
(4, 10, 5, 2),
(5, 10, 5, 2),
(6, 10, 5, 2),
(7, 10, 5, 2),
(7.49f, 10, 5, 2),
(7.5f, 10, 5, 3),
(6.66f, 10, 5, 2),
(6.67f, 10, 5, 3),
(7, 10, 5, 3),
(8, 10, 5, 3),
(9, 10, 5, 3),
(10, 10, 5, 4),
Expand Down

0 comments on commit de74e0f

Please sign in to comment.