DoubleComparator.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /*
  3. * This file is part of sebastian/comparator.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Comparator;
  11. /**
  12. * Compares doubles for equality.
  13. */
  14. class DoubleComparator extends NumericComparator
  15. {
  16. /**
  17. * Smallest value available in PHP.
  18. *
  19. * @var float
  20. */
  21. const EPSILON = 0.0000000001;
  22. /**
  23. * Returns whether the comparator can compare two values.
  24. *
  25. * @param mixed $expected The first value to compare
  26. * @param mixed $actual The second value to compare
  27. *
  28. * @return bool
  29. */
  30. public function accepts($expected, $actual)
  31. {
  32. return (\is_float($expected) || \is_float($actual)) && \is_numeric($expected) && \is_numeric($actual);
  33. }
  34. /**
  35. * Asserts that two values are equal.
  36. *
  37. * @param mixed $expected First value to compare
  38. * @param mixed $actual Second value to compare
  39. * @param float $delta Allowed numerical distance between two values to consider them equal
  40. * @param bool $canonicalize Arrays are sorted before comparison when set to true
  41. * @param bool $ignoreCase Case is ignored when set to true
  42. *
  43. * @throws ComparisonFailure
  44. */
  45. public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)
  46. {
  47. if ($delta == 0) {
  48. $delta = self::EPSILON;
  49. }
  50. parent::assertEquals($expected, $actual, $delta, $canonicalize, $ignoreCase);
  51. }
  52. }