Skip to content
This repository has been archived by the owner on Jan 6, 2020. It is now read-only.

Commit

Permalink
Merge 1ff0978 into 56a646b
Browse files Browse the repository at this point in the history
  • Loading branch information
terrafrost committed Jun 21, 2019
2 parents 56a646b + 1ff0978 commit f8a93a2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 9 deletions.
24 changes: 23 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,43 @@ matrix:
allow_failures:
- php: nightly
include:
- php: 5.3
- php: 5.4
- php: 5.4
env:
- BCMATH="off"
- php: 5.5
- php: 5.5
env:
- BCMATH="off"
- php: 5.6
- php: 5.6
env:
- BCMATH="off"
- php: 7.0
- php: 7.0
env:
- BCMATH="off"
- php: 7.1
env:
- TEST_COVERAGE=true
- php: 7.1
env:
- BCMATH="off"
- php: nightly
- php: hhvm

sudo: false

before_install:
- if [[ $BCMATH == "off" ]]; then
export PHP_BUILD_CONFIGURE_OPTS="--disable-bcmath";
git clone git://github.com/php-build/php-build.git $HOME/.phpenv/plugins/php-build;
travis_wait phpenv install --force `php -r "echo PHP_VERSION;"`;
phpenv rehash;
phpenv global `php -r "echo PHP_VERSION;"`;
fi
- if [[ $TEST_COVERAGE != 'true' && "$(php --version | grep xdebug -ci)" -ge 1 ]]; then phpenv config-rm xdebug.ini ; fi
- php -i | grep bcmath

install:
- travis_retry composer install --no-interaction --prefer-dist
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ allowing one to perform mathematical calculations on numeric strings,
going well outside the integer range of the system and maintaining arbitrary
precision for more precise calculations.

Moontoast\Math requires PHP 5.3+ and the [bcmath extension][].

## Installation

The preferred method of installation is via [Composer][]:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"require": {
"php": ">=5.3.3",
"ext-bcmath": "*"
"phpseclib/bcmath_compat": ">=1.0.3"
},
"require-dev": {
"jakub-onderka/php-parallel-lint": "^0.9.0",
Expand Down
2 changes: 1 addition & 1 deletion src/Moontoast/Math/AbstractBigNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,6 @@ public static function convertToBase10($number, $fromBase)
*/
public static function setDefaultScale($scale)
{
ini_set('bcmath.scale', $scale);
bcscale($scale);
}
}
6 changes: 4 additions & 2 deletions src/Moontoast/Math/BigNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,11 @@ public function floor()
public function getScale()
{
if ($this->numberScale === null) {
return ini_get('bcmath.scale');
if (version_compare(PHP_VERSION, '7.3.0') >= 0 || !extension_loaded('bcmath')) {
return (string) bcscale();
}
return (string) max(0, strlen(bcadd('0', '0')) - 2);
}

return $this->numberScale;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Moontoast/Math/BigNumberImmutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class BigNumberImmutableImmutableTest extends TestCase
{
protected function setUp()
{
ini_set('bcmath.scale', 0);
bcscale(0);
}

/**
Expand Down Expand Up @@ -49,7 +49,7 @@ public function testConstruct()
*/
public function testWithScale()
{
ini_set('bcmath.scale', 5);
bcscale(5);

$bn = new BigNumberImmutable('9223372036854775808.12345');

Expand Down
11 changes: 11 additions & 0 deletions tests/Moontoast/Math/BigNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -817,4 +817,15 @@ public function testConvertToBase10AlwaysUsesZeroScale()
}
}
}

public function testBCScale()
{
BigNumber::setDefaultScale(2);
bcscale(0);

$bn = new BigNumber(5);
$decimal = preg_replace('#^5(?:\.)?#', '', $bn->getValue());

$this->assertSame(strlen($decimal), $bn->getScale());
}
}

0 comments on commit f8a93a2

Please sign in to comment.