/** * @dataProvider sumProvider */ public function testSum(DecimalValue $a, DecimalValue $b, $value) { $math = new DecimalMath(); $actual = $math->sum($a, $b); $this->assertEquals($value, $actual->getValue()); $actual = $math->sum($b, $a); $this->assertEquals($value, $actual->getValue()); }
/** * Returns a QuantityValue representing the given amount, automatically assuming * a level of uncertainty based on the digits given. * * The upper and lower bounds are determined automatically from the given * digits by increasing resp. decreasing the least significant digit. * E.g. "+0.01" would have upperBound "+0.02" and lowerBound "+0.01", * while "-100" would have upperBound "-99" and lowerBound "-101". * * @param DecimalValue $amount The quantity * @param string $unit The quantity's unit (use "1" for unit-less quantities) * @param DecimalValue $margin * * @return QuantityValue */ private function newUncertainQuantityFromMargin(DecimalValue $amount, $unit = '1', DecimalValue $margin) { $decimalMath = new DecimalMath(); $margin = $margin->computeAbsolute(); $lowerBound = $decimalMath->sum($amount, $margin->computeComplement()); $upperBound = $decimalMath->sum($amount, $margin); return new QuantityValue($amount, $unit, $upperBound, $lowerBound); }
/** * Returns a DecimalValue representing the symmetrical offset to be applied * to the raw amount for a rough representation of the uncertainty interval, * as in "amount +/- offset". * * The offset is calculated as max( amount - lowerBound, upperBound - amount ). * * @since 0.1 * * @return DecimalValue */ public function getUncertaintyMargin() { $math = new DecimalMath(); $lowerMargin = $math->sum($this->getAmount(), $this->getLowerBound()->computeComplement()); $upperMargin = $math->sum($this->getUpperBound(), $this->getAmount()->computeComplement()); $margin = $math->max($lowerMargin, $upperMargin); return $margin; }