-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Use predefined PHP float epsilon #4874
Conversation
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.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, @marijnvanwezel!
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
`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.
`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.
`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.
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:
Quote from @sebastianbergmann:
(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 self::assertSame('1.2345', sprintf('%.4f',12.345/10)); |
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.