Skip to content

Commit

Permalink
GMP error when comparing fractions (#598)
Browse files Browse the repository at this point in the history
* Add failing test GmpCalculatorTest::it_compares_zero_with_fraction()

* Fix GmpCalculator::compare() when comparing fractions less than 1
  • Loading branch information
hellodave76 committed Mar 18, 2020
1 parent 0415ec9 commit 122664c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Calculator/GmpCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public function compare($a, $b)
return $integersCompared;
}

return gmp_cmp($aNum->getFractionalPart(), $bNum->getFractionalPart());
$aNumFractional = $aNum->getFractionalPart() === '' ? '0' : $aNum->getFractionalPart();
$bNumFractional = $bNum->getFractionalPart() === '' ? '0' : $bNum->getFractionalPart();

return gmp_cmp($aNumFractional, $bNumFractional);
}

return gmp_cmp($a, $b);
Expand Down
8 changes: 8 additions & 0 deletions tests/Calculator/GmpCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ public function it_floors_zero()
{
$this->assertSame('0', $this->getCalculator()->floor('0'));
}

/**
* @test
*/
public function it_compares_zero_with_fraction()
{
$this->assertSame(1, $this->getCalculator()->compare('0.5', '0'));
}
}

0 comments on commit 122664c

Please sign in to comment.