You are here

function webform_compare_floats in Webform 7.4

Compare two floats.

See http://php.net/manual/en/language.types.float.php.

Comparison of floating point numbers for equality is surprisingly difficult, as evidenced by the references below. The simple test in this function works for numbers that are relatively close to 1E1. For very small numbers, it will show false equality. For very large numbers, it will show false inequality. Better implementations are hidered by the absense of PHP platform-specific floating point constants to properly set the minimum absolute and relative error in PHP.

The use case for webform conditionals excludes very small or very large numeric comparisons.

See http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm See http://floating-point-gui.de/errors/comparison/ See http://en.wikipedia.org/wiki/IEEE_754-1985#Denormalized_numbers

Parameters

float $number_1: The first number.

float $number_2: The second number.

Return value

int|null < 0 if number_1 is less than number_2; > 0 if number_1 is greater than number_2, 0 if they are equal, and NULL if either is not numeric.

6 calls to webform_compare_floats()
webform_conditional_operator_numeric_equal in includes/webform.conditionals.inc
Conditional callback for numeric comparisons.
webform_conditional_operator_numeric_greater_than in includes/webform.conditionals.inc
Conditional callback for numeric comparisons.
webform_conditional_operator_numeric_greater_than_equal in includes/webform.conditionals.inc
Conditional callback for numeric comparisons.
webform_conditional_operator_numeric_less_than in includes/webform.conditionals.inc
Conditional callback for numeric comparisons.
webform_conditional_operator_numeric_less_than_equal in includes/webform.conditionals.inc
Conditional callback for numeric comparisons.

... See full list

File

components/number.inc, line 949
Webform module number component.

Code

function webform_compare_floats($number_1, $number_2) {
  if (!is_numeric($number_1) || !is_numeric($number_2)) {
    return NULL;
  }
  $number_1 = (double) $number_1;
  $number_2 = (double) $number_2;
  $epsilon = 1.0E-6;
  if (abs($number_1 - $number_2) < $epsilon) {
    return 0;
  }
  elseif ($number_1 > $number_2) {
    return 1;
  }
  else {
    return -1;
  }
}