DoubleComparator.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /*
  3. * This file is part of the Comparator package.
  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. * @return bool
  28. */
  29. public function accepts($expected, $actual)
  30. {
  31. return (is_double($expected) || is_double($actual)) && is_numeric($expected) && is_numeric($actual);
  32. }
  33. /**
  34. * Asserts that two values are equal.
  35. *
  36. * @param mixed $expected First value to compare
  37. * @param mixed $actual Second value to compare
  38. * @param float $delta Allowed numerical distance between two values to consider them equal
  39. * @param bool $canonicalize Arrays are sorted before comparison when set to true
  40. * @param bool $ignoreCase Case is ignored when set to true
  41. *
  42. * @throws ComparisonFailure
  43. */
  44. public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)
  45. {
  46. if ($delta == 0) {
  47. $delta = self::EPSILON;
  48. }
  49. parent::assertEquals($expected, $actual, $delta, $canonicalize, $ignoreCase);
  50. }
  51. }