@@ -2,12 +2,12 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace Samsara\Fermat; |
| 4 | 4 | |
| 5 | -use Samsara\Exceptions\UsageError\IntegrityConstraint; |
|
| 6 | -use Samsara\Fermat\Values\Currency; |
|
| 7 | -use Samsara\Fermat\Values\ImmutableFraction; |
|
| 8 | -use Samsara\Fermat\Values\ImmutableNumber; |
|
| 9 | -use Samsara\Fermat\Values\MutableFraction; |
|
| 10 | -use Samsara\Fermat\Values\MutableNumber; |
|
| 5 | +use Samsara\Exceptions\UsageError\IntegrityConstraint; |
|
| 6 | +use Samsara\Fermat\Values\Currency; |
|
| 7 | +use Samsara\Fermat\Values\ImmutableFraction; |
|
| 8 | +use Samsara\Fermat\Values\ImmutableNumber; |
|
| 9 | +use Samsara\Fermat\Values\MutableFraction; |
|
| 10 | +use Samsara\Fermat\Values\MutableNumber; |
|
| 11 | 11 | use Samsara\Fermat\Types\Base\NumberInterface; |
| 12 | 12 | |
| 13 | 13 | class Numbers |
@@ -2,14 +2,14 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace Samsara\Fermat\Types; |
| 4 | 4 | |
| 5 | -use Ds\Hashable; |
|
| 6 | -use Samsara\Exceptions\SystemError\LogicalError\IncompatibleObjectState; |
|
| 7 | -use Samsara\Fermat\Numbers; |
|
| 8 | -use Samsara\Fermat\Provider\BCProvider; |
|
| 9 | -use Riimu\Kit\BaseConversion\BaseConverter; |
|
| 10 | -use Samsara\Fermat\Provider\SequenceProvider; |
|
| 11 | -use Samsara\Fermat\Provider\SeriesProvider; |
|
| 12 | -use Samsara\Fermat\Types\Base\NumberInterface; |
|
| 5 | +use Ds\Hashable; |
|
| 6 | +use Samsara\Exceptions\SystemError\LogicalError\IncompatibleObjectState; |
|
| 7 | +use Samsara\Fermat\Numbers; |
|
| 8 | +use Samsara\Fermat\Provider\BCProvider; |
|
| 9 | +use Riimu\Kit\BaseConversion\BaseConverter; |
|
| 10 | +use Samsara\Fermat\Provider\SequenceProvider; |
|
| 11 | +use Samsara\Fermat\Provider\SeriesProvider; |
|
| 12 | +use Samsara\Fermat\Types\Base\NumberInterface; |
|
| 13 | 13 | use Samsara\Fermat\Values\ImmutableNumber; |
| 14 | 14 | |
| 15 | 15 | abstract class Number implements Hashable |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | public function __construct($value, $precision = null, $base = 10) |
| 27 | 27 | {
|
| 28 | 28 | $this->base = $base; |
| 29 | - $this->value = (string)$value; |
|
| 29 | + $this->value = (string) $value; |
|
| 30 | 30 | |
| 31 | 31 | if (!is_null($precision)) {
|
| 32 | 32 | $this->precision = $precision; |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | $curVal = $this->getValue(); |
| 137 | 137 | $calcVal = Numbers::make(Numbers::IMMUTABLE, 1); |
| 138 | 138 | |
| 139 | - for ($i = 1;$i <= $curVal;$i++) {
|
|
| 139 | + for ($i = 1; $i <= $curVal; $i++) {
|
|
| 140 | 140 | $calcVal = $calcVal->multiply($i); |
| 141 | 141 | } |
| 142 | 142 | |
@@ -152,19 +152,19 @@ discard block |
||
| 152 | 152 | |
| 153 | 153 | if ($val->modulo(2)->isEqual(1)) {
|
| 154 | 154 | $m = Numbers::make(Numbers::IMMUTABLE, $val->add(1)->divide(2)); |
| 155 | - $term = function ($n) {
|
|
| 155 | + $term = function($n) {
|
|
| 156 | 156 | return Numbers::make(Numbers::IMMUTABLE, 2)->multiply($n); |
| 157 | 157 | }; |
| 158 | 158 | } else {
|
| 159 | 159 | $m = Numbers::make(Numbers::IMMUTABLE, $val->divide(2)); |
| 160 | - $term = function ($n) {
|
|
| 160 | + $term = function($n) {
|
|
| 161 | 161 | return Numbers::make(Numbers::IMMUTABLE, 2)->multiply($n)->subtract(1); |
| 162 | 162 | }; |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | $newVal = Numbers::makeOne(); |
| 166 | 166 | |
| 167 | - for ($i = 1;$m->isGreaterThanOrEqualTo($i);$i++) {
|
|
| 167 | + for ($i = 1; $m->isGreaterThanOrEqualTo($i); $i++) {
|
|
| 168 | 168 | $newVal = $newVal->multiply($term($i)); |
| 169 | 169 | } |
| 170 | 170 | |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | $oldBase = $this->convertForModification(); |
| 198 | 198 | |
| 199 | 199 | if ($this->isGreaterThanOrEqualTo(PHP_INT_MIN) && $this->isLessThanOrEqualTo(PHP_INT_MAX)) {
|
| 200 | - return $this->setValue(log((float)$this->getValue()))->convertFromModification($oldBase); |
|
| 200 | + return $this->setValue(log((float) $this->getValue()))->convertFromModification($oldBase); |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | $x = Numbers::make(Numbers::IMMUTABLE, $this->getValue(), $this->getPrecision()); |
@@ -269,15 +269,15 @@ discard block |
||
| 269 | 269 | return $this->setValue( |
| 270 | 270 | SeriesProvider::maclaurinSeries( |
| 271 | 271 | Numbers::make(Numbers::IMMUTABLE, $value), |
| 272 | - function ($n) {
|
|
| 272 | + function($n) {
|
|
| 273 | 273 | $negOne = Numbers::make(Numbers::IMMUTABLE, -1); |
| 274 | 274 | |
| 275 | 275 | return $negOne->pow($n); |
| 276 | 276 | }, |
| 277 | - function ($n) {
|
|
| 277 | + function($n) {
|
|
| 278 | 278 | return SequenceProvider::nthOddNumber($n); |
| 279 | 279 | }, |
| 280 | - function ($n) {
|
|
| 280 | + function($n) {
|
|
| 281 | 281 | return SequenceProvider::nthOddNumber($n)->factorial(); |
| 282 | 282 | }, |
| 283 | 283 | 0, |
@@ -315,13 +315,13 @@ discard block |
||
| 315 | 315 | return $this->setValue( |
| 316 | 316 | SeriesProvider::maclaurinSeries( |
| 317 | 317 | Numbers::make(Numbers::IMMUTABLE, $value), |
| 318 | - function ($n) {
|
|
| 318 | + function($n) {
|
|
| 319 | 319 | return SequenceProvider::nthPowerNegativeOne($n); |
| 320 | 320 | }, |
| 321 | - function ($n) {
|
|
| 321 | + function($n) {
|
|
| 322 | 322 | return SequenceProvider::nthEvenNumber($n); |
| 323 | 323 | }, |
| 324 | - function ($n) {
|
|
| 324 | + function($n) {
|
|
| 325 | 325 | return SequenceProvider::nthEvenNumber($n)->factorial(); |
| 326 | 326 | }, |
| 327 | 327 | 0, |
@@ -355,18 +355,18 @@ discard block |
||
| 355 | 355 | return $this->setValue( |
| 356 | 356 | SeriesProvider::maclaurinSeries( |
| 357 | 357 | Numbers::make(Numbers::IMMUTABLE, $value), |
| 358 | - function ($n) {
|
|
| 358 | + function($n) {
|
|
| 359 | 359 | $four = Numbers::make(Numbers::IMMUTABLE, 4); |
| 360 | 360 | $n = Numbers::makeOrDont(Numbers::IMMUTABLE, $n); |
| 361 | 361 | |
| 362 | 362 | return SequenceProvider::nthBernoulliNumber($n->multiply(2)->getValue(), -4)->pow($n)->multiply(Numbers::makeOne()->subtract($four->pow($n))); |
| 363 | 363 | }, |
| 364 | - function ($n) {
|
|
| 364 | + function($n) {
|
|
| 365 | 365 | $n = Numbers::makeOrDont(Numbers::IMMUTABLE, $n); |
| 366 | 366 | |
| 367 | 367 | return $n->multiply(2)->subtract(1); |
| 368 | 368 | }, |
| 369 | - function ($n) {
|
|
| 369 | + function($n) {
|
|
| 370 | 370 | return SequenceProvider::nthEvenNumber($n)->factorial(); |
| 371 | 371 | }, |
| 372 | 372 | 1, |
@@ -50,6 +50,9 @@ discard block |
||
| 50 | 50 | return $this->precision; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | + /** |
|
| 54 | + * @param NumberInterface $num |
|
| 55 | + */ |
|
| 53 | 56 | public function add($num) |
| 54 | 57 | {
|
| 55 | 58 | $num = Numbers::makeOrDont($this, $num, $this->getPrecision()); |
@@ -99,7 +102,7 @@ discard block |
||
| 99 | 102 | * Note about precision: it uses the smaller of the two precisions (significant figures). |
| 100 | 103 | * |
| 101 | 104 | * @param $num |
| 102 | - * @return mixed |
|
| 105 | + * @return NumberInterface |
|
| 103 | 106 | */ |
| 104 | 107 | public function divide($num) |
| 105 | 108 | {
|
@@ -550,6 +553,9 @@ discard block |
||
| 550 | 553 | } |
| 551 | 554 | } |
| 552 | 555 | |
| 556 | + /** |
|
| 557 | + * @param integer $value |
|
| 558 | + */ |
|
| 553 | 559 | public function isGreaterThan($value): bool |
| 554 | 560 | {
|
| 555 | 561 | $value = Numbers::makeOrDont(Numbers::IMMUTABLE, $value, $this->getPrecision()); |
@@ -582,6 +588,9 @@ discard block |
||
| 582 | 588 | } |
| 583 | 589 | } |
| 584 | 590 | |
| 591 | + /** |
|
| 592 | + * @param integer $value |
|
| 593 | + */ |
|
| 585 | 594 | public function isLessThanOrEqualTo($value): bool |
| 586 | 595 | {
|
| 587 | 596 | $value = Numbers::makeOrDont(Numbers::IMMUTABLE, $value, $this->getPrecision()); |
@@ -21,6 +21,10 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | protected $symbol; |
| 23 | 23 | |
| 24 | + /** |
|
| 25 | + * @param integer $symbol |
|
| 26 | + * @param integer $precision |
|
| 27 | + */ |
|
| 24 | 28 | public function __construct($value, $symbol = Currency::DOLLAR, $precision = null, $base = 10) |
| 25 | 29 | { |
| 26 | 30 | $this->symbol = $symbol; |
@@ -63,7 +67,7 @@ discard block |
||
| 63 | 67 | } |
| 64 | 68 | |
| 65 | 69 | /** |
| 66 | - * @param $value |
|
| 70 | + * @param NumberInterface $value |
|
| 67 | 71 | * |
| 68 | 72 | * @return Currency |
| 69 | 73 | */ |