-
-
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
TestCase::assertSame
(and related exact comparisons) must compare float
exactly
#4966
Comments
TestCase::assertSame
(and related exact comparisons) must compare scalar types binaryTestCase::assertSame
(and related exact comparisons) must compare float
exactly
Feel free to send a pull request for the By the way, your example works for me with
|
the issue is fixed in 136fae3 since phpunit 8.5.24/9.5.14 I propose to remove https://github.com/sebastianbergmann/phpunit/blob/8.5/src/Framework/Constraint/IsIdentical.php#L63 if condition completely like: - 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; as I am not aware of any valid use case it should handle |
HI @mvorisek, with this change, I have some float comparison which are failing. Basically with some things like
Wouldn't be useful to provide the old implementation of assertEquals to another method ? |
Hi @VincentLanglet, I am for it, but it is not that easy to do it correctly. The old implementation was dependent on
Also note there is |
This issue and associated PR caused one of our tests to fail which exposed an underlying bug to us, so just wanted to say thanks for your work on reporting and fixing this 👍 🙌 |
+1 ^ 🙌 🖖 |
I was expecting that using $this->assertEqualsWithDelta($expected, $actual, PHP_FLOAT_EPSILON); But my tests are still failing when they were passing before. Edit: $this->assertEqualsWithDelta($expected, $actual, 10 ** -ini_get('precision')); |
@Seb33300 // pass
$this->assertEquals(
0.0000000000000001 + 0.0000000000000002,
0.0000000000000003,
);
// pass
$this->assertEquals(
(string) (0.1 + 0.2),
(string) 0.3,
);
// fail
$this->assertEquals(
0.1 + 0.2,
0.3,
); Any code that you have that is relying on This is the way I'm going forward with patching tests because the delta calculation |
I'm confused. Why is the issue closed if the problem still exists? |
Summary
Currently test case like:
pass even if the numbers are different. They are really stored differently, test case:
fails correctly.
It seems phpunit converts float numbers using default php float to string conversion which depends on php.ini
precision
configuration, test case like:fails as expected.
Also, the float diff output has currently limited precision.
To fix exact comparisons & diff output, I propose to convert float numbers to string using function like:
The text was updated successfully, but these errors were encountered: