CustomVariableNumber: tolerant float comparison

This commit is contained in:
Thomas Gelf 2016-10-06 16:18:34 +00:00
parent f71bfb1204
commit 2c071e215d

View File

@ -6,22 +6,25 @@ use Icinga\Exception\ProgrammingError;
class CustomVariableNumber extends CustomVariable class CustomVariableNumber extends CustomVariable
{ {
// Hint: 'F' is intentional, this MUST NOT respect locales
const PRECISION = '%.9F';
public function equals(CustomVariable $var) public function equals(CustomVariable $var)
{ {
if (! $var instanceof CustomVariableNumber) { if (! $var instanceof CustomVariableNumber) {
return false; return false;
} }
// TODO: in case we encounter problems with floats we could $cur = $this->getValue();
// consider something as follows, but taking more care $new = $var->getValue();
// about precision:
/* // Be tolerant when comparing floats:
if (is_float($this->value)) { if (is_float($cur) || is_float($new)) {
return sprintf($var->getValue(), '%.9F') return sprintf(self::PRECISION, $cur)
=== sprintf($this->getValue(), '%.9F'); === sprintf(self::PRECISION, $new);
} }
*/
return $var->getValue() === $this->getValue(); return $cur === $new;
} }
public function getDbFormat() public function getDbFormat()
@ -59,8 +62,7 @@ class CustomVariableNumber extends CustomVariable
if (is_int($this->value)) { if (is_int($this->value)) {
return (string) $this->value; return (string) $this->value;
} else { } else {
// Hint: this MUST NOT respect locales return sprintf(self::PRECISION, $this->value);
return sprintf('%F', $this->value);
} }
} }
} }