Skip to content

Commit

Permalink
Merge 9898953 into 3a9f378
Browse files Browse the repository at this point in the history
  • Loading branch information
mariushoch committed Oct 29, 2018
2 parents 3a9f378 + 9898953 commit 7e383e1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/DataValues/DecimalMath.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public function getUseBC() {
public function product( DecimalValue $a, DecimalValue $b ) {
if ( $this->useBC ) {
$scale = strlen( $a->getFractionalPart() ) + strlen( $b->getFractionalPart() );
// Decimal values must not be longer than 127 chars
$maxScale = 127 - ( strlen( $a->getIntegerPart() ) + strlen( $b->getIntegerPart() ) + 4 );

$scale = min( $scale, $maxScale );
$product = bcmul( $a->getValue(), $b->getValue(), $scale );
} else {
$product = $a->getValueFloat() * $b->getValueFloat();
Expand Down
9 changes: 8 additions & 1 deletion tests/DataValues/DecimalMathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public function slumpProvider() {
* @dataProvider productProvider
*/
public function testProduct( DecimalValue $a, DecimalValue $b, $value ) {
$math = new DecimalMath();
// FIXME: This should set $useBC to false (bcmath is tested below)
$math = new DecimalMath( /* $useBC */ true );

$actual = $math->product( $a, $b );
$this->assertSame( $value, $actual->getValue() );
Expand Down Expand Up @@ -134,6 +135,12 @@ public function productWithBCProvider() {
return [
[ new DecimalValue( '+0.1' ), new DecimalValue( '+0.1' ), '+0.01' ],
[ new DecimalValue( '-5000000' ), new DecimalValue( '-0.1' ), '+500000.0' ],

[
new DecimalValue( '+0.' . str_repeat( '3', 124 ) ),
new DecimalValue( '+0.' . str_repeat( '6', 124 ) ),
'+0.' . str_repeat( '2', 127 - 6 )
],
];
}

Expand Down

0 comments on commit 7e383e1

Please sign in to comment.