/** * 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 int $exponent Decimal exponent to apply * * @return QuantityValue */ private function newUncertainQuantityFromDigits(DecimalValue $amount, $unit = '1', $exponent = 0) { $math = new DecimalMath(); if ($amount->getSign() === '+') { $upperBound = $math->bump($amount); $lowerBound = $math->slump($amount); } else { $upperBound = $math->slump($amount); $lowerBound = $math->bump($amount); } $amount = $this->decimalParser->applyDecimalExponent($amount, $exponent); $lowerBound = $this->decimalParser->applyDecimalExponent($lowerBound, $exponent); $upperBound = $this->decimalParser->applyDecimalExponent($upperBound, $exponent); return new QuantityValue($amount, $unit, $upperBound, $lowerBound); }
/** * Shift the decimal point according to the given exponent. * * @param DecimalValue $decimal * @param int $exponent The exponent to apply (digits to shift by). A Positive exponent * shifts the decimal point to the right, a negative exponent shifts to the left. * * @throws InvalidArgumentException * @return DecimalValue */ public function shift(DecimalValue $decimal, $exponent) { if (!is_int($exponent)) { throw new InvalidArgumentException('$exponent must be an integer'); } if ($exponent == 0) { return $decimal; } $sign = $decimal->getSign(); $intPart = $decimal->getIntegerPart(); $fractPart = $decimal->getFractionalPart(); if ($exponent < 0) { $intPart = $this->shiftLeft($intPart, $exponent); } else { $fractPart = $this->shiftRight($fractPart, $exponent); } $digits = $sign . $intPart . $fractPart; $digits = $this->stripLeadingZeros($digits); return new DecimalValue($digits); }
/** * @dataProvider getSignProvider */ public function testGetSign(DecimalValue $value, $expected) { $actual = $value->getSign(); $this->assertSame($expected, $actual); }