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

Drop comparison /w PHP_FLOAT_EPSILON #4972

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
13 changes: 1 addition & 12 deletions src/Framework/Constraint/IsIdentical.php
Expand Up @@ -9,13 +9,8 @@
*/
namespace PHPUnit\Framework\Constraint;

use const PHP_FLOAT_EPSILON;
use function abs;
use function get_class;
use function is_array;
use function is_float;
use function is_infinite;
use function is_nan;
use function is_object;
use function is_string;
use function sprintf;
Expand Down Expand Up @@ -60,13 +55,7 @@ public function __construct($value)
*/
public function evaluate($other, string $description = '', bool $returnResult = false)
{
if (is_float($this->value) && is_float($other) &&
!is_infinite($this->value) && !is_infinite($other) &&
!is_nan($this->value) && !is_nan($other)) {
$success = abs($this->value - $other) < PHP_FLOAT_EPSILON;
} else {
$success = $this->value === $other;
}
$success = $this->value === $other;

if ($returnResult) {
return $success;
Expand Down
22 changes: 19 additions & 3 deletions tests/unit/Framework/AssertTest.php
Expand Up @@ -2418,9 +2418,15 @@ protected function sameValues(): array
// integers
[0, 0],
// floats
[1.0, 1.0],
[2.3, 2.3],
[1 / 3, 1 - 2 / 3],
[1 / 3, 1 / 3],
[1 - 2 / 3, 1 - 2 / 3],
[5.5E+123, 5.5E+123],
[5.5E-123, 5.5E-123],
[log(0), log(0)],
[INF, INF],
[-INF, -INF],
// arrays
[[], []],
[[0 => 1], [0 => 1]],
Expand Down Expand Up @@ -2474,6 +2480,12 @@ protected function notEqualValues(): array
[[[2.3]], [[4.2]], 0.5],
[new Struct(2.3), new Struct(4.2), 0.5],
[[new Struct(2.3)], [new Struct(4.2)], 0.5],
[1 / 3, 1 - 2 / 3],
[1 / 3, '0.33333333333333337'],
[1 - 2 / 3, '3333333333333333'],
[5.5E+123, 5.6E+123],
[5.5E-123, 5.6E-123],
[5.5E+123, 5.5E-123],
// NAN
[NAN, NAN],
// arrays
Expand Down Expand Up @@ -2648,8 +2660,12 @@ protected function equalValues(): array
['0', 0],
[2.3, '2.3'],
['2.3', 2.3],
[(string) (1 / 3), 1 - 2 / 3],
[1 / 3, (string) (1 - 2 / 3)],
[1, 1.0],
[1.0, '1'],
[1 / 3, '0.3333333333333333'],
[1 - 2 / 3, '0.33333333333333337'],
[5.5E+123, '5.5E+123'],
[5.5E-123, '5.5E-123'],
['string representation', new ClassWithToString],
[new ClassWithToString, 'string representation'],
];
Expand Down