Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix comparisons #3

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions features/parse.feature
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ Feature: Parse Method Implementation
| 18 | 4 ** .5 | 1.000000000000000000 |
| 0 | 4 ^ .5 | 2 |
| 18 | 4 ^ .5 | 2.000000000000000000 |
| 0 | 1 = 5 | false |
| 18 | 1 = 5 | false |
| 0 | 1 == 5 | false |
| 18 | 1 == 5 | false |
| 0 | 1 > 5 | false |
| 18 | 1 > 5 | false |
| 0 | 1 = 5 | 0 |
| 18 | 1 = 5 | 0 |
| 0 | 1 == 5 | 0 |
| 18 | 1 == 5 | 0 |
| 0 | 1 > 5 | 0 |
| 18 | 1 > 5 | 0 |
| 0 | 1 < 5 | true |
| 18 | 1 < 5 | true |
| 0 | 1 >= 5 | false |
| 18 | 1 >= 5 | false |
| 0 | 1 >= 5 | 0 |
| 18 | 1 >= 5 | 0 |
| 0 | 1 <= 5 | true |
| 18 | 1 <= 5 | true |
| 0 | 1 <> 5 | true |
Expand Down
22 changes: 12 additions & 10 deletions src/BC.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public static function round($val, $scale = null)
* @param integer|null $scale The scale to pass to BC::math methods
* @return string|integer|float|boolean
*/
public static function parse($formula, $values = [], $scale = null)
public static function parse($formula, $values = [], $scale = null, $returnBool = false)
{
$scale = static::getScale($scale);

Expand Down Expand Up @@ -433,17 +433,17 @@ public static function parse($formula, $values = [], $scale = null)
case '**': $result = static::pow($opTrio[1], $opTrio[3], $scale); break;
case '^': $result = static::powfrac($opTrio[1], $opTrio[3], $scale); break;
case '==':
case '=': $result = static::comp($opTrio[1], $opTrio[3], $scale) == 0; break;
case '>': $result = static::comp($opTrio[1], $opTrio[3], $scale) > 0; break;
case '<': $result = static::comp($opTrio[1], $opTrio[3], $scale) < 0; break;
case '>=': $result = static::comp($opTrio[1], $opTrio[3], $scale) >= 0; break;
case '<=': $result = static::comp($opTrio[1], $opTrio[3], $scale) <= 0; break;
case '=': $result = static::comp($opTrio[1], $opTrio[3], $scale) == 0 ? 1:0; break;
case '>': $result = static::comp($opTrio[1], $opTrio[3], $scale) > 0 ? 1:0; break;
case '<': $result = static::comp($opTrio[1], $opTrio[3], $scale) < 0 ? 1:0; break;
case '>=': $result = static::comp($opTrio[1], $opTrio[3], $scale) >= 0 ? 1:0; break;
case '<=': $result = static::comp($opTrio[1], $opTrio[3], $scale) <= 0 ? 1:0; break;
case '<>':
case '!=': $result = static::comp($opTrio[1], $opTrio[3], $scale) != 0; break;
case '!=': $result = static::comp($opTrio[1], $opTrio[3], $scale) != 0 ? 1:0; break;
case '|':
case '||': $result = ($opTrio[1] || $opTrio[3]); break;
case '||': $result = ($opTrio[1] || $opTrio[3]) ? 1:0; break;
case '&':
case '&&': $result = ($opTrio[1] && $opTrio[3]); break;
case '&&': $result = ($opTrio[1] && $opTrio[3]) ? 1:0; break;
case '~':
case '~~': $result = (bool) ((bool) $opTrio[1] ^ (bool) $opTrio[3]); break;
}
Expand All @@ -454,7 +454,9 @@ public static function parse($formula, $values = [], $scale = null)
// error_log("Replacing {$parenthetical[0]} with {$parenthetical[1]} in {$formula}");
$formula = str_replace($parenthetical[0], $parenthetical[1], $formula);
}

if($returnBool) {
return (bool)$formula;
}
return $formula;
}

Expand Down
25 changes: 13 additions & 12 deletions tests/BCTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,18 @@ public function testParse()
$this->assertEquals(4.5, BC::parse('5.5 -% 2'));
$this->assertEquals(1, BC::parse('4 ** .5'));
$this->assertEquals(2, BC::parse('4 ^ .5'));
$this->assertEquals(false, BC::parse('1 = 5'));
$this->assertEquals(false, BC::parse('1 == 5'));
$this->assertEquals(false, BC::parse('1 > 5'));
$this->assertEquals(true, BC::parse('1 < 5'));
$this->assertEquals(false, BC::parse('1 >= 5'));
$this->assertEquals(true, BC::parse('1 <= 5'));
$this->assertEquals(true, BC::parse('1 <> 5'));
$this->assertEquals(true, BC::parse('1 != 5'));
$this->assertEquals(true, BC::parse('1 & 5'));
$this->assertEquals(true, BC::parse('1 && 5'));
$this->assertEquals(true, BC::parse('1 | 5'));
$this->assertEquals(true, BC::parse('1 || 5'));
$this->assertEquals(0, BC::parse('1 = 5'));
$this->assertEquals(0, BC::parse('1 == 5'));
$this->assertEquals(0, BC::parse('1 > 5'));
$this->assertEquals(1, BC::parse('1 < 5'));
$this->assertEquals(0, BC::parse('1 >= 5'));
$this->assertEquals(1, BC::parse('1 <= 5'));
$this->assertEquals(1, BC::parse('1 <> 5'));
$this->assertEquals(1, BC::parse('1 != 5'));
$this->assertEquals(1, BC::parse('1 & 5'));
$this->assertEquals(1, BC::parse('1 && 5'));
$this->assertEquals(1, BC::parse('1 | 5'));
$this->assertEquals(1, BC::parse('1 || 5'));
$this->assertEquals(false, BC::parse('1 ~ 5'));
$this->assertEquals(false, BC::parse('1 ~~ 5'));
$this->assertEquals('2.25', BC::parse('5 / 4 + 1'));
Expand All @@ -225,6 +225,7 @@ public function testParse()
$this->assertEquals('1', BC::parse('(1 + 5) / 4', null, 0));
$this->assertEquals('1.5', BC::parse('({a} + {b}) / {c}', ['a' => 1, 'b' => 5, 'c' => 4]));
$this->assertEquals('1', BC::parse('({a} + {b}) / {c}', ['a' => 1, 'b' => 5, 'c' => 4], 0));
$this->assertTrue(BC::parse('1', [], 0, true));
}

/**
Expand Down