FloatComparator.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\Math;
  8. /**
  9. * Contains methods to compare float digits.
  10. *
  11. * @api
  12. * @since 101.0.6
  13. */
  14. class FloatComparator
  15. {
  16. /**
  17. * Precision for floats comparing.
  18. *
  19. * @var float
  20. */
  21. private static $epsilon = 0.00001;
  22. /**
  23. * Compares two float digits.
  24. *
  25. * @param float $a
  26. * @param float $b
  27. * @return bool
  28. * @since 101.0.6
  29. */
  30. public function equal(float $a, float $b): bool
  31. {
  32. return abs($a - $b) <= self::$epsilon;
  33. }
  34. /**
  35. * Compares if the first argument greater than the second argument.
  36. *
  37. * @param float $a
  38. * @param float $b
  39. * @return bool
  40. * @since 101.0.6
  41. */
  42. public function greaterThan(float $a, float $b): bool
  43. {
  44. return ($a - $b) > self::$epsilon;
  45. }
  46. /**
  47. * Compares if the first argument greater or equal to the second.
  48. *
  49. * @param float $a
  50. * @param float $b
  51. * @return bool
  52. * @since 101.0.6
  53. */
  54. public function greaterThanOrEqual(float $a, float $b): bool
  55. {
  56. return $this->equal($a, $b) || $this->greaterThan($a, $b);
  57. }
  58. }