Skip to content

Commit 93fe694

Browse files
committed
Better identification for perfect sets of integer constants
1 parent 0e6f308 commit 93fe694

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

IntegerConversion.ConstantEstimation/Program.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,28 @@ private static void EstimateConstants(int originalBitCount, int newBitCount, out
5454
if (ConvertEstimate(originalMaxValue, ia, ib, ic) == newMaxValue)
5555
{
5656
float error = 0;
57+
bool anyIncorrect = false;
5758
for (uint x = 1; x < originalMaxValue; x++)
5859
{
5960
uint y = ConvertEstimate(x, ia, ib, ic);
6061
float yExact = (float)x * newMaxValue / originalMaxValue;
62+
uint yExactRounded = (uint)float.Round(yExact);
63+
anyIncorrect |= (y != yExactRounded);
6164
float diff = yExact - y;
6265
error += diff * diff;
6366
}
67+
if (!anyIncorrect)
68+
{
69+
a = ia;
70+
b = ib;
71+
c = ic;
72+
return;
73+
}
6474
if (error < bestError)
6575
{
6676
a = ia;
6777
b = ib;
6878
c = ic;
69-
if (error == 0)//Huge performance savings for when newBitCount / originalBitCount has no remainder.
70-
{
71-
return;
72-
}
7379
bestError = error;
7480
}
7581
}

0 commit comments

Comments
 (0)