DoubleComparator.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php declare(strict_types=1);
  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. use function is_float;
  12. use function is_numeric;
  13. /**
  14. * Compares doubles for equality.
  15. *
  16. * @deprecated since v3.0.5 and v4.0.8
  17. */
  18. class DoubleComparator extends NumericComparator
  19. {
  20. /**
  21. * Smallest value available in PHP.
  22. *
  23. * @var float
  24. */
  25. public const EPSILON = 0.0000000001;
  26. /**
  27. * Returns whether the comparator can compare two values.
  28. *
  29. * @param mixed $expected The first value to compare
  30. * @param mixed $actual The second value to compare
  31. *
  32. * @return bool
  33. */
  34. public function accepts($expected, $actual)
  35. {
  36. return (is_float($expected) || is_float($actual)) && is_numeric($expected) && is_numeric($actual);
  37. }
  38. /**
  39. * Asserts that two values are equal.
  40. *
  41. * @param mixed $expected First value to compare
  42. * @param mixed $actual Second value to compare
  43. * @param float $delta Allowed numerical distance between two values to consider them equal
  44. * @param bool $canonicalize Arrays are sorted before comparison when set to true
  45. * @param bool $ignoreCase Case is ignored when set to true
  46. *
  47. * @throws ComparisonFailure
  48. */
  49. public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)/*: void*/
  50. {
  51. if ($delta == 0) {
  52. $delta = self::EPSILON;
  53. }
  54. parent::assertEquals($expected, $actual, $delta, $canonicalize, $ignoreCase);
  55. }
  56. }