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

Most upvoted comments

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() on float values does not make sense.

I do not remember why private const EPSILON = 0.0000000001 is defined and used in the IsIdentical constraint that is used by assertSame(). To be honest, until I looked the code of IsIdentical 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 on float 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).