aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorClément Lafont <johnrazeur@gmail.com>2020-02-07 19:05:26 +0300
committerGitHub <noreply@github.com>2020-02-07 19:05:26 +0300
commit43790a456e8ec4104895869b178165fec65613d7 (patch)
tree7e00d08ffef0673b4ece2d1a547008779bbe43cb /src
parent707e22029d8bcfc93918eaca27905d14e376d3b5 (diff)
Fix substraction by zero bug (#56)
Diffstat (limited to 'src')
-rw-r--r--src/NXP/Classes/Lexer.php16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/NXP/Classes/Lexer.php b/src/NXP/Classes/Lexer.php
index 73c25ac..cd12d21 100644
--- a/src/NXP/Classes/Lexer.php
+++ b/src/NXP/Classes/Lexer.php
@@ -78,8 +78,8 @@ class Lexer
// if the number starts with a minus sign, it could be a negative number, or it could be an operator grabbed by the greedy regex
// if previous token is an operator, then it negative, otherwise remove the minus sign and put a negative operator on the stack
if ($lastToken !== null) {
- $value = (int)$token->getValue();
- if ($value < 0 && ! ($lastToken instanceof AbstractOperator)) {
+ $value = $token->getValue();
+ if (($value < 0 || $this->isNegativeZero($value)) && ! ($lastToken instanceof AbstractOperator)) {
$token = new TokenNumber(abs($value));
$output[] = $token;
$output[] = new TokenMinus('-');
@@ -145,4 +145,16 @@ class Lexer
return $output;
}
+
+ /**
+ * Check if the value is a negative zero
+ *
+ * @param int|float $x The value to check
+ * @return boolean True if negative zero, false otherwise
+ */
+ private function isNegativeZero($x)
+ {
+ $floatVal = floatval($x);
+ return $floatVal === 0.0 && $floatVal ** -1 === -INF;
+ }
}