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

Fix float comparison precision #102

Merged
merged 1 commit into from Sep 14, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/DoubleComparator.php
Expand Up @@ -11,6 +11,8 @@

/**
* Compares doubles for equality.
*
* @deprecated since v3.0.5 and v4.0.8
*/
class DoubleComparator extends NumericComparator
sebastianbergmann marked this conversation as resolved.
Show resolved Hide resolved
{
Expand Down
1 change: 0 additions & 1 deletion src/Factory.php
Expand Up @@ -123,7 +123,6 @@ private function registerDefaultComparators()
$this->registerDefaultComparator(new ObjectComparator);
$this->registerDefaultComparator(new ResourceComparator);
$this->registerDefaultComparator(new ArrayComparator);
$this->registerDefaultComparator(new DoubleComparator);
$this->registerDefaultComparator(new NumericComparator);
$this->registerDefaultComparator(new ScalarComparator);
$this->registerDefaultComparator(new TypeComparator);
Expand Down
4 changes: 1 addition & 3 deletions src/NumericComparator.php
Expand Up @@ -24,10 +24,8 @@ class NumericComparator extends ScalarComparator
*/
public function accepts($expected, $actual)
{
// all numerical values, but not if one of them is a double
// or both of them are strings
// all numerical values, but not if both of them are strings
return \is_numeric($expected) && \is_numeric($actual) &&
!(\is_float($expected) || \is_float($actual)) &&
!(\is_string($expected) && \is_string($actual));
}

Expand Down
2 changes: 2 additions & 0 deletions tests/DoubleComparatorTest.php
Expand Up @@ -12,6 +12,8 @@
use PHPUnit\Framework\TestCase;

/**
* @deprecated since v3.0.5 and v4.0.8
*
* @covers \SebastianBergmann\Comparator\DoubleComparator<extended>
*
* @uses \SebastianBergmann\Comparator\Comparator
Expand Down
6 changes: 3 additions & 3 deletions tests/FactoryTest.php
Expand Up @@ -37,9 +37,9 @@ public function instanceProvider()
['0', 0, 'SebastianBergmann\\Comparator\\NumericComparator'],
[0, '0', 'SebastianBergmann\\Comparator\\NumericComparator'],
[0, 0, 'SebastianBergmann\\Comparator\\NumericComparator'],
[1.0, 0, 'SebastianBergmann\\Comparator\\DoubleComparator'],
[0, 1.0, 'SebastianBergmann\\Comparator\\DoubleComparator'],
[1.0, 1.0, 'SebastianBergmann\\Comparator\\DoubleComparator'],
[1.0, 0, 'SebastianBergmann\\Comparator\\NumericComparator'],
[0, 1.0, 'SebastianBergmann\\Comparator\\NumericComparator'],
[1.0, 1.0, 'SebastianBergmann\\Comparator\\NumericComparator'],
[[1], [1], 'SebastianBergmann\\Comparator\\ArrayComparator'],
[$tmpfile, $tmpfile, 'SebastianBergmann\\Comparator\\ResourceComparator'],
[new \stdClass, new \stdClass, 'SebastianBergmann\\Comparator\\ObjectComparator'],
Expand Down
51 changes: 47 additions & 4 deletions tests/NumericComparatorTest.php
Expand Up @@ -37,18 +37,30 @@ public function acceptsSucceedsProvider()
[8, '0'],
['10', 0],
[0x74c3b00c, 42],
[0755, 0777]
[0755, 0777],
[8, 5.0],
[5.0, 8],
[5, 5],
['4.5', 5],
[0x539, 02471],
[0, 5.0],
[5.0, 0],
['5', 4.5],
[1.2e3, 7E-10],
[3, \acos(8)],
[\acos(8), 3],
[\acos(8), \acos(8)],
];
}

public function acceptsFailsProvider()
{
return [
['5', '10'],
[8, 5.0],
[5.0, 8],
[10, null],
[false, 12]
[false, 12],
[5.0, false],
[null, 5.0],
];
}

Expand All @@ -61,6 +73,22 @@ public function assertEqualsSucceedsProvider()
[02471, 1337],
[1337, 1338, 1],
['1337', 1340, 5],
[2.3, 2.3],
['2.3', 2.3],
[5.0, 5],
[5, 5.0],
[5.0, '5'],
[1.2e3, 1200],
[2.3, 2.5, 0.5],
[3, 3.05, 0.05],
[1.2e3, 1201, 1],
[1 / 3, '0.3333333333333333'],
[1 - 2 / 3, '0.33333333333333337'],
[1 / 3, 1 - 2 / 3, 0.0000000001],
[5.5E+123, '5.5E+123'],
[5.5E-123, '5.5E-123'],
[5.5E+123, '5.6E+123', 0.2E+123],
[5.5E-123, '5.6E-123', 0.2E-123],
];
}

Expand All @@ -72,6 +100,21 @@ public function assertEqualsFailsProvider()
[0x539, 1338],
[1337, 1339, 1],
['1337', 1340, 2],
[2.3, 4.2],
['2.3', 4.2],
[5.0, '4'],
[5.0, 6],
[1.2e3, 1201],
[2.3, 2.5, 0.2],
[3, 3.05, 0.04],
[3, \acos(8)],
[\acos(8), 3],
[\acos(8), \acos(8)],
[1 / 3, 1 - 2 / 3],
[5.5E+123, '5.7E+123'],
[5.5E-123, '5.7E-123'],
[5.5E+123, '5.7E+123', 0.1E+123],
[5.5E-123, '5.7E-123', 0.1E-123],
];
}

Expand Down