phpunit: assertSame and assertEquals incorrectly pass on some floats
Q | A |
---|---|
PHPUnit version | 7.1.5 |
PHP version | 7.2.5 |
Installation Method | Composer |
These two assertions pass:
self::assertEquals(0.06999999999, 0.07);
self::assertSame(0.06999999999, 0.07);
They shouldn’t.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 2
- Comments: 23 (13 by maintainers)
Commits related to this issue
- Make epsilon for float comparison customizable - #3159 — committed to SebastianKull/phpunit by deleted user 5 years ago
The more I think about it the more I come to the conclusion that two floating point numbers cannot (really) be compared for identity and that using
assertSame()
onfloat
values does not make sense.I do not remember why
private const EPSILON = 0.0000000001
is defined and used in theIsIdentical
constraint that is used byassertSame()
. To be honest, until I looked the code ofIsIdentical
today, I was not even aware of that.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.Coming back to
assertSame()
, I think that it might make sense to simply disallow its use onfloat
values. Thoughts?yes - https://3v4l.org/570q1 - and 👍 for the PR!
This issue can probably be closed because of #4874. This fix also does not break any backwards-compatibility promises.
Bump?
Eventually: yes. This will take time (for thinking and discussing).