I have an expression that is used to estimate percentiles by interpolating between two values.
windowMin + (currentPercentile - lastPercentile) * (windowMax - windowMin) / (percentile - lastPercentile)
This has given me very good real-world results. However, in my unit tests, I'm having trouble assering that things are working correctly, since I consistently get significant rounding error.
In three test cases, I try to get the 40th, 50th and 60th percentile, resulting in these computations:
1 + (0.4 - 0.3333333333333333) * (2 - 1) / (0.6666666666666666 - 0.3333333333333333)
1 + (0.5 - 0.3333333333333333) * (2 - 1) / (0.6666666666666666 - 0.3333333333333333)
1 + (0.6 - 0.3333333333333333) * (2 - 1) / (0.6666666666666666 - 0.3333333333333333)
This yields:
{
"0.4": 1.2000000000000002,
"0.5": 1.5,
"0.6": 1.8
}
This fails my assertion, which is looking for 1.2
for the 40th percentile.
Is there a way to restructure this expression to improve accuracy in all cases? If not, is there an easy way to work around this issue with chai assertions?