moneyphp / money

PHP implementation of Fowler's Money pattern.
http://moneyphp.org
MIT License
4.63k stars 441 forks source link

GMP error when dividing by a float between 0 and 1 #597

Closed hellodave76 closed 4 years ago

hellodave76 commented 4 years ago

Some code to reproduce it -

use Money\Money;

$value = Money::GBP(1);
// Actual divisor value not important, just has to be a float and less than 1.
$result = $value->divide(0.5);

var_dump($result);

expected result, something like -

class Money\Money#3 (2) {
  private $amount =>
  string(9) "2"
  private $currency =>
  class Money\Currency#2 (1) {
    private $code =>
    string(3) "GBP"
  }
}

actual result -

PHP Warning:  gmp_cmp(): Unable to convert variable to GMP - string is not an integer in /home/me/Documents/Projects/moneytest/vendor/moneyphp/money/src/Calculator/GmpCalculator.php on line 56
PHP Stack trace:
PHP   1. {main}() /home/me/Documents/Projects/moneytest/test.php:0
PHP   2. Money\Money->divide() /home/me/Documents/Projects/moneytest/test.php:8
PHP   3. Money\Calculator\GmpCalculator->compare() /home/me/Documents/Projects/moneytest/vendor/moneyphp/money/src/Money.php:338
PHP   4. gmp_cmp() /home/me/Documents/Projects/moneytest/vendor/moneyphp/money/src/Calculator/GmpCalculator.php:56

PHP version 7.4.3