public function testPositiveNegativeDecimalAdd() { $n1 = Decimal::fromString('3.45'); $n2 = Decimal::fromString('-7.67'); $this->assertTrue($n1->add($n2)->equals(Decimal::fromString('-4.22'))); $this->assertTrue($n2->add($n1)->equals(Decimal::fromString('-4.22'))); }
public function testNegativeNumbers() { $this->assertFalse(Decimal::fromInteger(-1)->isZero()); $this->assertFalse(Decimal::fromFloat(-1.0)->isZero()); $this->assertFalse(Decimal::fromFloat(-0.1)->isZero()); $this->assertFalse(Decimal::fromString('-1')->isZero()); }
/** * @dataProvider modProvider */ public function testFiniteFiniteMod($number, $mod, $answer, $scale = null) { $numberDec = Decimal::fromString($number); $modDec = Decimal::fromString($mod); $decimalAnswer = $numberDec->mod($modDec, $scale); $this->assertTrue(Decimal::fromString($answer)->equals($decimalAnswer), $decimalAnswer . ' % ' . $mod . ' must be equal to ' . $answer . ', but was ' . $decimalAnswer); }
public function testBasicCase() { $n1 = Decimal::fromString('3.45'); $this->assertTrue(Decimal::fromDecimal($n1)->equals($n1)); $this->assertTrue(Decimal::fromDecimal($n1, 2)->equals($n1)); $this->assertTrue(Decimal::fromDecimal($n1, 1)->equals(Decimal::fromString('3.5'))); $this->assertTrue(Decimal::fromDecimal($n1, 0)->equals(Decimal::fromString('3'))); }
function handleValueFromDb($value, array $fieldInfo, $row) { if ($value === null && $this->zeroIfNull) { $value = "0"; } if ($value !== null) { return BigNumbers\Decimal::fromString($value); } }
public function __invoke($value) : ValidationResult { Assertion::numeric($value); $decimalValue = Decimal::fromString((string) $value); if ($decimalValue->comp($this->limit) === 1) { return new ValidationResult(new ValidationError('error.max-number', ['limit' => (string) $this->limit])); } return new ValidationResult(); }
public function __invoke($value) : ValidationResult { Assertion::numeric($value); $decimalValue = Decimal::fromString((string) $value); $floorModulo = $this->floorModulo($decimalValue->sub($this->base), $this->step); if ($floorModulo->comp(DecimalConstants::zero()) !== 0) { return new ValidationResult(new ValidationError('error.step-number', ['lowValue' => $this->trimZeroDecimal((string) $decimalValue->sub($floorModulo)), 'highValue' => $this->trimZeroDecimal((string) $decimalValue->add($this->step)->sub($floorModulo))])); } return new ValidationResult(); }
public function testAsFloat() { $this->assertEquals(1.0, Decimal::fromString('1.0')->asFloat()); $this->assertTrue(is_float(Decimal::fromString('1.0')->asFloat())); $this->assertEquals(1.0, Decimal::fromInteger(1)->asFloat()); $this->assertTrue(is_float(Decimal::fromInteger(1)->asFloat())); $this->assertEquals(1.0, Decimal::fromFloat(1.0)->asFloat()); $this->assertEquals(1.123123123, Decimal::fromString('1.123123123')->asFloat()); $this->assertTrue(is_float(Decimal::fromFloat(1.0)->asFloat())); $this->assertTrue(is_float(Decimal::fromString('1.123123123')->asFloat())); }
public function testBasicDiv() { $one = Decimal::fromInteger(1); $two = Decimal::fromInteger(2); $four = Decimal::fromInteger(4); $eight = Decimal::fromInteger(8); // Integer exact division $this->assertTrue($eight->div($two)->equals($four)); $this->assertTrue($eight->div($four)->equals($two)); // Arbitrary precision division $this->assertTrue($one->div($eight, 0)->equals(Decimal::fromString('0'))); $this->assertTrue($one->div($eight, 1)->equals(Decimal::fromString('0.1'))); $this->assertTrue($one->div($eight, 2)->equals(Decimal::fromString('0.13'))); $this->assertTrue($one->div($eight, 3)->equals(Decimal::fromString('0.125'))); }
public function __invoke(string $value) { $cleanedValue = preg_replace_callback('(^[^\\d-.]*(-?)([^.]*)(.?)(.*)$)', function (array $match) : string { return $match[1] . preg_replace('([^\\d]+)', '', $match[2]) . $match[3] . preg_replace('([^\\d]+)', '', $match[4]); }, $value); if ('' === $cleanedValue) { return null; } if ('-' === $cleanedValue) { $cleanedValue = '0'; } if (0 === strpos($cleanedValue, '.')) { $cleanedValue = '0' . $cleanedValue; } return Decimal::fromString($cleanedValue); }
public function testNotIntegers() { $this->assertFalse(Decimal::fromString("-200.001")->isInteger()); $this->assertFalse(Decimal::fromString("-2.001")->isInteger()); $this->assertFalse(Decimal::fromString("-1.001")->isInteger()); $this->assertFalse(Decimal::fromString("0.001")->isInteger()); $this->assertFalse(Decimal::fromString("1.001")->isInteger()); $this->assertFalse(Decimal::fromString("2.001")->isInteger()); $this->assertFalse(Decimal::fromString("200.001")->isInteger()); $this->assertFalse(Decimal::fromFloat(-200.001)->isInteger()); $this->assertFalse(Decimal::fromFloat(-2.001)->isInteger()); $this->assertFalse(Decimal::fromFloat(-1.001)->isInteger()); $this->assertFalse(Decimal::fromFloat(0.001)->isInteger()); $this->assertFalse(Decimal::fromFloat(1.001)->isInteger()); $this->assertFalse(Decimal::fromFloat(2.001)->isInteger()); $this->assertFalse(Decimal::fromFloat(200.001)->isInteger()); }
function testDecimalToDb() { $d = Test\Factory::managerNoteModelCustom(' /** :amiss = true; */ class Pants { /** :amiss = {"field": {"primary": true, "type": "autoinc"}}; */ public $id; /** :amiss = {"field": {"type": "decimal"}}; */ public $num; } '); $c = $d->ns . '\\Pants'; $obj = new $c(); $obj->num = Decimal::fromString("1.23456"); $d->manager->insert($obj); $out = $d->connector->query("SELECT num FROM pants")->fetchColumn(0); $this->assertEquals("1.23456", $out); }
public function testScaledEquals() { // Transitivity $this->assertTrue(Decimal::fromFloat(1.001)->equals(Decimal::fromFloat(1.01), 1)); $this->assertTrue(Decimal::fromFloat(1.01)->equals(Decimal::fromFloat(1.004), 1)); $this->assertTrue(Decimal::fromFloat(1.001)->equals(Decimal::fromFloat(1.004), 1)); // Reflexivity $this->assertTrue(Decimal::fromFloat(1.00525)->equals(Decimal::fromFloat(1.00525), 2)); // Symmetry $this->assertTrue(Decimal::fromFloat(1.01)->equals(Decimal::fromFloat(1.001), 1)); $this->assertTrue(Decimal::fromFloat(1.004)->equals(Decimal::fromFloat(1.01), 1)); $this->assertTrue(Decimal::fromFloat(1.004)->equals(Decimal::fromFloat(1.001), 1)); // Proper rounding $this->assertTrue(Decimal::fromFloat(1.004)->equals(Decimal::fromFloat(1.0), 2)); // Warning, float to Decimal conversion can have unexpected behaviors, like converting // 1.005 to Decimal("1.0049999999999999") $this->assertTrue(Decimal::fromFloat(1.0050000000001)->equals(Decimal::fromFloat(1.01), 2)); $this->assertTrue(Decimal::fromString("1.005")->equals(Decimal::fromString("1.010"), 2)); }
/** * @dataProvider tanProvider */ public function testSimple($nr, $answer, $digits) { $x = Decimal::fromString($nr); $tanX = $x->tan($digits); $this->assertTrue(Decimal::fromString($answer)->equals($tanX), 'tan(' . $nr . ') must be equal to ' . $answer . ', but was ' . $tanX); }
public function testWithScale() { $this->assertTrue(Decimal::fromString('7.426', 2)->equals(Decimal::fromString('7.43'))); }
public function testE() { $this->assertTrue(DecimalConstants::e()->equals(Decimal::fromString("2.71828182845904523536028747135266"))); $this->assertTrue(DecimalConstants::e(32)->equals(Decimal::fromString("2.71828182845904523536028747135266"))); $this->assertTrue(DecimalConstants::e(16)->equals(Decimal::fromString("2.7182818284590452"))); }
public function testNoUsefulFloor() { $this->assertTrue(Decimal::fromString('3.45')->floor(2)->equals(Decimal::fromString('3.45'))); $this->assertTrue(Decimal::fromString('3.45')->floor(3)->equals(Decimal::fromString('3.45'))); }
public static function validXmlProvider() : array { return ['project-sample-data' => ['projectsampledata.xml', [['record-id' => 7676, 'mod-id' => 5, 'PROJECT ID MATCH FIELD' => Decimal::fromInteger(1), 'Created By' => 'Tim Thomson', 'Creation TimeStamp' => new DateTimeImmutable('2012-02-22 17:19:47 UTC'), 'Project Name' => 'Launch web site', 'Description' => "Launch the web site with our new branding and product line.\n\n" . " Third line", 'Status' => Decimal::fromInteger(4), 'Status on Screen' => 'Overdue', 'Start Date' => new DateTimeImmutable('2011-04-13 00:00:00 UTC'), 'Due Date' => new DateTimeImmutable('2012-05-02 00:00:00 UTC'), 'Days Remaining' => Decimal::fromInteger(0), 'Days Elapsed' => Decimal::fromInteger(275), 'Project Completion' => Decimal::fromString('0.48'), 'Tag' => 'marketing', 'Start Date Project Completion' => new DateTimeImmutable('2011-04-13 00:00:00 UTC'), 'Due Date Project Completion' => new DateTimeImmutable('2012-05-02 00:00:00 UTC'), 'Repeating Field' => ['A1', 'B2', 'C3', 'D4', 'E5', 'F6', 'G7', 'H8', 'I9'], 'Tasks' => [['record-id' => 14999, 'mod-id' => 1, 'Task Name' => 'Site map sketch', 'TASK ID MATCH FIELD' => Decimal::fromInteger(2), 'Repeating Field' => [Decimal::fromInteger(1), Decimal::fromInteger(2), Decimal::fromInteger(3)]], ['record-id' => 15000, 'mod-id' => 1, 'Task Name' => 'Send art to vendor', 'TASK ID MATCH FIELD' => Decimal::fromInteger(4), 'Repeating Field' => [Decimal::fromInteger(4), Decimal::fromInteger(5), Decimal::fromInteger(6)]], ['record-id' => 15001, 'mod-id' => 1, 'Task Name' => 'Review mock ups', 'TASK ID MATCH FIELD' => Decimal::fromInteger(5), 'Repeating Field' => [Decimal::fromInteger(7), Decimal::fromInteger(8), Decimal::fromInteger(9)]], ['record-id' => 15002, 'mod-id' => 0, 'Task Name' => 'Write page text', 'TASK ID MATCH FIELD' => Decimal::fromInteger(6), 'Repeating Field' => [null, null, null]], ['record-id' => 15003, 'mod-id' => 0, 'Task Name' => 'New logo art', 'TASK ID MATCH FIELD' => Decimal::fromInteger(3), 'Repeating Field' => [null, null, null]]]], ['record-id' => 7677, 'mod-id' => 4, 'PROJECT ID MATCH FIELD' => Decimal::fromInteger(7), 'Created By' => 'Tim Thomson', 'Creation TimeStamp' => new DateTimeImmutable('2012-02-22 17:19:47 UTC'), 'Project Name' => 'Prototype', 'Description' => "Build a working prototype of the new product.\n\n\n" . " Fourth line.", 'Status' => Decimal::fromInteger(4), 'Status on Screen' => 'Overdue', 'Start Date' => new DateTimeImmutable('2012-02-06 00:00:00 UTC'), 'Due Date' => new DateTimeImmutable('2012-02-17 00:00:00 UTC'), 'Days Remaining' => Decimal::fromInteger(0), 'Days Elapsed' => Decimal::fromInteger(9), 'Project Completion' => Decimal::fromString('0.32'), 'Tag' => 'manufacturing', 'Start Date Project Completion' => new DateTimeImmutable('2012-02-09 00:00:00 UTC'), 'Due Date Project Completion' => new DateTimeImmutable('2012-02-17 00:00:00 UTC'), 'Repeating Field' => ['*1', '-2', '+3', '.4', '/5', '=6', '=7', '-8', '`9'], 'Tasks' => [['record-id' => 15005, 'mod-id' => 1, 'Task Name' => 'Build prototype', 'TASK ID MATCH FIELD' => Decimal::fromInteger(9), 'Repeating Field' => [null, null, null]], ['record-id' => 15006, 'mod-id' => 1, 'Task Name' => 'Review sketches', 'TASK ID MATCH FIELD' => Decimal::fromInteger(8), 'Repeating Field' => [null, null, null]], ['record-id' => 15007, 'mod-id' => 1, 'Task Name' => 'Complete sketches', 'TASK ID MATCH FIELD' => Decimal::fromInteger(7), 'Repeating Field' => [null, null, null]], ['record-id' => 15014, 'mod-id' => 0, 'Task Name' => 'Draft requirements', 'TASK ID MATCH FIELD' => Decimal::fromInteger(16), 'Repeating Field' => [null, null, null]], ['record-id' => 15015, 'mod-id' => 0, 'Task Name' => 'Review requirements', 'TASK ID MATCH FIELD' => Decimal::fromInteger(17), 'Repeating Field' => [null, null, null]]]], ['record-id' => 7678, 'mod-id' => 4, 'PROJECT ID MATCH FIELD' => Decimal::fromInteger(13), 'Created By' => 'Tim Thomson', 'Creation TimeStamp' => new DateTimeImmutable('2012-02-22 17:19:47 UTC'), 'Project Name' => 'Investor meeting', 'Description' => "This is important. We need the investors to have confidence.\n" . " Second line.", 'Status' => Decimal::fromInteger(4), 'Status on Screen' => 'Overdue', 'Start Date' => new DateTimeImmutable('2011-12-12 00:00:00 UTC'), 'Due Date' => new DateTimeImmutable('2012-03-22 00:00:00 UTC'), 'Days Remaining' => Decimal::fromInteger(0), 'Days Elapsed' => Decimal::fromInteger(73), 'Project Completion' => Decimal::fromString('0.4285714285714286'), 'Tag' => 'finance', 'Start Date Project Completion' => new DateTimeImmutable('2012-01-02 00:00:00 UTC'), 'Due Date Project Completion' => new DateTimeImmutable('2012-03-22 00:00:00 UTC'), 'Repeating Field' => ['a1', 'b2', 'c3', 'd4', 'e5', 'f6', 'g7', 'h8', 'i9'], 'Tasks' => [['record-id' => 15004, 'mod-id' => 1, 'Task Name' => 'Gather requirements', 'TASK ID MATCH FIELD' => Decimal::fromInteger(1), 'Repeating Field' => [null, Decimal::fromInteger(0), null]], ['record-id' => 15008, 'mod-id' => 1, 'Task Name' => 'Investor meeting', 'TASK ID MATCH FIELD' => Decimal::fromInteger(13), 'Repeating Field' => [null, null, Decimal::fromInteger(0)]], ['record-id' => 15009, 'mod-id' => 1, 'Task Name' => 'Final draft of slides', 'TASK ID MATCH FIELD' => Decimal::fromInteger(12), 'Repeating Field' => [null, null, null]], ['record-id' => 15010, 'mod-id' => 0, 'Task Name' => 'Complete business plan', 'TASK ID MATCH FIELD' => Decimal::fromInteger(10), 'Repeating Field' => [null, null, null]], ['record-id' => 15011, 'mod-id' => 0, 'Task Name' => 'First draft of slides', 'TASK ID MATCH FIELD' => Decimal::fromInteger(11), 'Repeating Field' => [null, null, null]], ['record-id' => 15012, 'mod-id' => 0, 'Task Name' => 'Market research', 'TASK ID MATCH FIELD' => Decimal::fromInteger(14), 'Repeating Field' => [null, null, null]], ['record-id' => 15013, 'mod-id' => 0, 'Task Name' => 'Competitive analysis', 'TASK ID MATCH FIELD' => Decimal::fromInteger(15), 'Repeating Field' => [null, null, null]]]]], 3], 'empty-resultset' => ['sample_fmresultset_empty.xml', [], 0]]; }
/** * Returns the Silver Ratio. * @return Decimal */ public static function silverRatio() { if (self::$SilverRatio === null) { self::$SilverRatio = Decimal::fromString("2.41421356237309504880168872420970"); } return self::$SilverRatio; }
/** * @expectedException \DomainException * @expectedExceptionMessage The arccos of this number is undefined. */ public function testArccosFewerThanNegativeOne() { Decimal::fromString('-304.75')->arccos(); }
/** * @dataProvider expProvider */ public function testSimple($nr, $answer, $digits) { $x = Decimal::fromString($nr); $expX = $x->exp((int) $digits); $this->assertTrue(Decimal::fromString($answer)->equals($expX), "The answer must be " . $answer . ", but was " . $expX); }
/** * @expectedException \DomainException * @expectedExceptionMessage The arcsecant of this number is undefined. */ public function testArcsecBetweenOneAndNegativeOne() { Decimal::fromString('0.546')->arcsec(); }
public function parameterProvider() : array { return ['no-parameters' => [[], '-lay=foo'], 'empty-parameter' => [['foo' => ''], '-lay=foo&foo'], 'null-parameter' => [['foo' => null], '-lay=foo&foo'], 'string-parameter' => [['foo' => 'bar'], '-lay=foo&foo=bar'], 'integer-parameter' => [['foo' => 3], '-lay=foo&foo=3'], 'float-parameter' => [['foo' => 3.3], '-lay=foo&foo=3.3'], 'boolean-parameter' => [['foo' => true], '-lay=foo&foo=1'], 'datetime-parameter' => [['foo' => new DateTimeImmutable('2016-01-01 00:00:00 UTC')], '-lay=foo&foo=01%2F01%2F2016+00%3A00%3A00'], 'decimal-parameter' => [['foo' => Decimal::fromString('12.499734362638823')], '-lay=foo&foo=12.499734362638823']]; }
public function testNoUsefulCeil() { $this->assertTrue(Decimal::fromString('3.45')->ceil(2)->equals(Decimal::fromString('3.45'))); $this->assertTrue(Decimal::fromString('3.45')->ceil(3)->equals(Decimal::fromString('3.45'))); }
public function testPositiveAdditiveInverse() { $this->assertTrue(Decimal::fromInteger(1)->additiveInverse()->equals(Decimal::fromInteger(-1))); $this->assertTrue(Decimal::fromString('1.768')->additiveInverse()->equals(Decimal::fromString('-1.768'))); }
/** * Calculates the cosine of this method with the highest possible accuracy * Note that accuracy is limited by the accuracy of predefined PI; * * @param integer $scale * @return Decimal cos($this) */ public function cos($scale = null) { // First normalise the number in the [0, 2PI] domain $x = $this->mod(DecimalConstants::PI()->mul(Decimal::fromString("2"))); // PI has only 32 significant numbers $scale = $scale === null ? 32 : $scale; return self::factorialSerie($x, DecimalConstants::one(), function ($i) { return $i % 2 === 0 ? $i % 4 === 0 ? DecimalConstants::one() : DecimalConstants::negativeOne() : DecimalConstants::zero(); }, $scale); }
protected function applyValue($input, Context $ctx) { $output = $input; cast: $allowString = $this->allowString === null ? true : $this->allowString; try { $cast = $input; if ($allowString && is_string($input)) { if ($input === '') { $cast = null; } else { $cast = BigNumbers\Decimal::fromString($input); } } elseif ($this->allowInt && is_int($input)) { $cast = BigNumbers\Decimal::fromInteger($input); } elseif ($this->allowDouble && is_float($input)) { $cast = BigNumbers\Decimal::fromFloat($input); } if ($cast !== $input) { $ctx->setChange(Change::Internal); } $output = $cast; } catch (\Exception $ex) { $ctx->addReason($this, ['id' => 'decimal.invalid']); goto done; } // must cast before this so we can cast to null if ($output === null) { goto done; } type: if (!$output instanceof BigNumbers\Decimal) { $ctx->addReason($this, ['id' => 'decimal.invalid']); goto done; } scale: if ($this->scale !== null) { if (preg_match('/\\.([0-9]+)$/', $output . '', $match)) { $inScale = strlen($match[1]); if ($inScale > $this->scale) { $ctx->addReason($this, ['id' => 'decimal.scale', 'params' => ['scale' => $inScale, 'expected' => $this->scale]]); } } } precision: if ($this->precision !== null) { $digits = preg_replace("/[^0-9]/", '', $output . ''); $inPrecision = strlen($digits); if ($inPrecision > $this->precision) { $ctx->addReason($this, ['id' => 'decimal.precision', 'params' => ['precision' => $inPrecision, 'expected' => $this->precision]]); } } minmax: $min = $this->min !== null ? BigNumbers\Decimal::create($this->min) : null; $max = $this->max !== null ? BigNumbers\Decimal::create($this->max) : null; if ($min !== null && $max !== null) { if ($output->comp($min) < 0 || $output->comp($max) > 0) { $ctx->addReason($this, ['id' => 'decimal.between', 'params' => ['atLeast' => $min . '', 'atMost' => $max . '']]); } } elseif ($min !== null) { if ($output->comp($min) < 0) { $ctx->addReason($this, ['id' => 'decimal.atLeast', 'params' => ['atLeast' => $min . '']]); } } elseif ($max !== null) { if ($output->comp($max) > 0) { $ctx->addReason($this, ['id' => 'decimal.atMost', 'params' => ['atMost' => $max . '']]); } } divisibleBy: if ($this->divisibleBy !== null) { $divisibleBy = !$this->divisibleBy instanceof BigNumbers\Decimal ? BigNumbers\Decimal::create($this->divisibleBy) : $this->divisibleBy; if (!$output->mod($divisibleBy)->isZero()) { $dvFmt = $this->removeTrailingZeroes($divisibleBy); $ctx->addReason($this, ['id' => 'decimal.divisibleBy', 'params' => ['divisibleBy' => $dvFmt]]); } } done: if ($this->emitString && $output instanceof BigNumbers\Decimal) { $output = $output->innerValue(); if (!is_string($input)) { $ctx->setChange(Change::Internal); } } return $output; }
public function testNegativeWithNegativeExponent() { $nFive = Decimal::fromInteger(-5); $this->assertTrue($nFive->pow(Decimal::fromInteger(-1))->equals(Decimal::fromString("-0.2")), "The answer must be -0.2, but was " . $nFive->pow(Decimal::fromInteger(-1))); $this->assertTrue($nFive->pow(Decimal::fromInteger(-2))->equals(Decimal::fromString("0.04"))); $this->assertTrue($nFive->pow(Decimal::fromInteger(-3))->equals(Decimal::fromString("-0.008"))); }
public function testCreateFromDecimal() { $this->assertTrue(Decimal::create(Decimal::fromString('345.76'), 1)->equals(Decimal::fromString('345.8'))); $this->assertTrue(Decimal::create(Decimal::fromString('345.76'), 2)->equals(Decimal::fromString('345.76'))); }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage $scale must be a positive integer */ public function testConstructorNotIntegerScaleValidation() { Decimal::fromString("25", "hola mundo"); }