Compare.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
  3. use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Exception;
  5. class Compare
  6. {
  7. use ArrayEnabled;
  8. /**
  9. * DELTA.
  10. *
  11. * Excel Function:
  12. * DELTA(a[,b])
  13. *
  14. * Tests whether two values are equal. Returns 1 if number1 = number2; returns 0 otherwise.
  15. * Use this function to filter a set of values. For example, by summing several DELTA
  16. * functions you calculate the count of equal pairs. This function is also known as the
  17. * Kronecker Delta function.
  18. *
  19. * @param array|float $a the first number
  20. * Or can be an array of values
  21. * @param array|float $b The second number. If omitted, b is assumed to be zero.
  22. * Or can be an array of values
  23. *
  24. * @return array|int|string (string in the event of an error)
  25. * If an array of numbers is passed as an argument, then the returned result will also be an array
  26. * with the same dimensions
  27. */
  28. public static function DELTA($a, $b = 0.0)
  29. {
  30. if (is_array($a) || is_array($b)) {
  31. return self::evaluateArrayArguments([self::class, __FUNCTION__], $a, $b);
  32. }
  33. try {
  34. $a = EngineeringValidations::validateFloat($a);
  35. $b = EngineeringValidations::validateFloat($b);
  36. } catch (Exception $e) {
  37. return $e->getMessage();
  38. }
  39. return (int) (abs($a - $b) < 1.0e-15);
  40. }
  41. /**
  42. * GESTEP.
  43. *
  44. * Excel Function:
  45. * GESTEP(number[,step])
  46. *
  47. * Returns 1 if number >= step; returns 0 (zero) otherwise
  48. * Use this function to filter a set of values. For example, by summing several GESTEP
  49. * functions you calculate the count of values that exceed a threshold.
  50. *
  51. * @param array|float $number the value to test against step
  52. * Or can be an array of values
  53. * @param null|array|float $step The threshold value. If you omit a value for step, GESTEP uses zero.
  54. * Or can be an array of values
  55. *
  56. * @return array|int|string (string in the event of an error)
  57. * If an array of numbers is passed as an argument, then the returned result will also be an array
  58. * with the same dimensions
  59. */
  60. public static function GESTEP($number, $step = 0.0)
  61. {
  62. if (is_array($number) || is_array($step)) {
  63. return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $step);
  64. }
  65. try {
  66. $number = EngineeringValidations::validateFloat($number);
  67. $step = EngineeringValidations::validateFloat($step ?? 0.0);
  68. } catch (Exception $e) {
  69. return $e->getMessage();
  70. }
  71. return (int) ($number >= $step);
  72. }
  73. }