Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use smaller epsilon and relative error for float comparison #1570

Merged
merged 3 commits into from Sep 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -59,6 +59,13 @@
*/
final class TestLocationBucketSorterTest extends TestCase
{
/**
* Used for floating point comparisons.
*
* @var float
*/
private const EPSILON = 0.001;

public function test_it_sorts(): void
{
$testLocation = new TestLocation('', '', 0.0);
Expand Down Expand Up @@ -189,7 +196,7 @@ public function test_it_sorts_faster_than_quicksort(ArrayIterator $uniqueTestLoc
$totalQuickSort += microtime(true) - $start;
}

$this->assertGreaterThanOrEqual(0.01, abs($totalQuickSort - $totalBucketSort));
$this->assertGreaterThanOrEqual(self::EPSILON, self::getRelativeError($totalQuickSort, $totalBucketSort));
}

public static function locationsArrayProvider(): iterable
Expand All @@ -206,6 +213,22 @@ static function (float $executionTime): TestLocation {
yield 'All locations' => [new ArrayIterator($locations)];
}

/**
* Finds relative error.
*
* @see https://floating-point-gui.de/errors/comparison/
* @see https://stackoverflow.com/questions/4915462/how-should-i-do-floating-point-comparison
*/
private static function getRelativeError(float $a, float $b): float
{
// We do not expect A or B to be extremely small or large: these are edge cases,
// and they will need special handling which we avoid simplicity sake.
self::assertGreaterThan(self::EPSILON, abs($a));
self::assertGreaterThan(self::EPSILON, abs($b));

return abs($a - $b) / (abs($a) + abs($b));
}

private static function quicksort(&$uniqueTestLocations): void
{
usort(
Expand Down