Skip to content

Commit

Permalink
Drop comparison /w PHP_FLOAT_EPSILON
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored and sebastianbergmann committed Sep 14, 2022
1 parent 3e5a5d0 commit b439136
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
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

0 comments on commit b439136

Please sign in to comment.