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 predefined PHP float epsilon #4874

Merged
merged 1 commit into from Jan 24, 2022
Merged

Use predefined PHP float epsilon #4874

merged 1 commit into from Jan 24, 2022

Conversation

marijnvanwezel
Copy link
Contributor

Instead of using an arbitrary epsilon, use the predefined PHP constant PHP_FLOAT_EPSILON, which is defined to be the smallest representable positive number "x", such that "x + 1.0 != 1.0".

See https://www.php.net/manual/en/reserved.constants.php#constant.php-float-epsilon for additional information.

Instead of using an arbitrary epsilon, use the predefined PHP constant PHP_FLOAT_EPSILON, which is defined to be the smallest representable positive number "x", such that "x + 1.0 != 1.0".

See https://www.php.net/manual/en/reserved.constants.php#constant.php-float-epsilon for additional information.
@sebastianbergmann sebastianbergmann merged commit 136fae3 into sebastianbergmann:8.5 Jan 24, 2022
@sebastianbergmann
Copy link
Owner

Thanks!

@@ -67,7 +62,7 @@ public function evaluate($other, string $description = '', bool $returnResult =
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) < self::EPSILON;
$success = abs($this->value - $other) < PHP_FLOAT_EPSILON;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marijnvanwezel marijnvanwezel deleted the use-epsilon branch January 26, 2022 15:35
nicolas-grekas added a commit to symfony/symfony that referenced this pull request Feb 18, 2022
This PR was merged into the 4.4 branch.

Discussion
----------

[Stopwatch] Fix test expectation

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

This test doesn't pass anymore because of sebastianbergmann/phpunit#4874 released in PHPUnit 9.5.14.

Commits
-------

11c1381 [Stopwatch] Fix test expectation
rez1dent3 added a commit to bavix/laravel-wallet that referenced this pull request Feb 18, 2022
Mellthas added a commit to dompdf/dompdf that referenced this pull request Mar 3, 2022
`assertSame` uses a stricter epsilon since version 9.5.14 (absolute
`PHP_FLOAT_EPSILON`), see sebastianbergmann/phpunit#4874.
Just rely on the recently introduced `Helpers::lengthEqual` for length
comparison.
Mellthas added a commit to dompdf/dompdf that referenced this pull request Mar 3, 2022
`assertSame` uses a stricter epsilon since version 9.5.14 (absolute
`PHP_FLOAT_EPSILON`), see sebastianbergmann/phpunit#4874.
Just rely on the recently introduced `Helpers::lengthEqual` for length
comparison.
Mellthas added a commit to dompdf/dompdf that referenced this pull request Mar 4, 2022
`assertSame` uses a stricter epsilon since version 9.5.14 (absolute
`PHP_FLOAT_EPSILON`), see sebastianbergmann/phpunit#4874.
Just rely on the recently introduced `Helpers::lengthEqual` for length
comparison.
@alfredbez
Copy link
Sponsor

In case someone stumbles upon this: This was breaking one assertion in our project which looked like this:

self::assertSame(1.2345, (12.345/10));

Here's the failure message:

Failed asserting that 1.2345000000000002 is identical to 1.2345.

Quote from @sebastianbergmann:

assertEquals() should not be used for floating point values either, at least not without its optional $delta parameter (in case the PHPUnit version that is used still has that). This optional parameter is deprecated in current versions and will be removed in the future. assertEqualsWithDelta() should be used in its stead.

(source, take also a look at the docs)

Changing this like so fixed the problem:

self::assertEqualsWithDelta(1.2345, (12.345/10), 0.0001);

Another way would be to convert that to a string using sprintf, depends on the use-case of course:

self::assertSame('1.2345', sprintf('%.4f',12.345/10));

sanmai added a commit to sanmai/pipeline that referenced this pull request Sep 2, 2022
eerison pushed a commit to eerison/rector-phpunit that referenced this pull request Sep 26, 2023
eerison pushed a commit to eerison/rector-phpunit that referenced this pull request Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants