From 9dba8dbe95c92af4b831bd6a80d87d0169147793 Mon Sep 17 00:00:00 2001 From: macocci7 Date: Sat, 11 Nov 2023 13:29:48 +0900 Subject: [PATCH] #19: #20: #21: all of these issues are reflected. - src/loader.php: removed. - src/Fraction.php: fixed bugs @int() @mixed() - src/Number.php: improved performance of functions named is***All(). - tests/*Test.php: updated. - composer.json: updated (version: 1.0.2 => 1.0.3). --- composer.json | 2 +- src/Fraction.php | 24 ++++++++++++++++-------- src/Number.php | 35 ++++++++++++++++++++--------------- src/loader.php | 10 ---------- tests/BezoutTest.php | 5 ----- tests/DivisorTest.php | 3 --- tests/EuclidTest.php | 3 --- tests/FractionTest.php | 37 ++++++++++++++++++++++++------------- tests/MultipleTest.php | 4 ---- tests/NumberTest.php | 3 +-- tests/PrimeTest.php | 2 -- 11 files changed, 62 insertions(+), 66 deletions(-) delete mode 100644 src/loader.php diff --git a/composer.json b/composer.json index 30a4a59..cb28a67 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "macocci7/php-math-integer", - "version": "1.0.2", + "version": "1.0.3", "description": "PHP Math Library of the subjects of number theory(only natural number).", "type": "library", "license": "MIT", diff --git a/src/Fraction.php b/src/Fraction.php index f56e305..53da6c1 100644 --- a/src/Fraction.php +++ b/src/Fraction.php @@ -293,15 +293,13 @@ public function improper() public function mixed() { if ( - $this->n->isNaturalAll( - [ - $this->numerator, - $this->denominator, - ] - ) + $this->n->isInt($this->numerator) + && $this->n->isNatural($this->denominator) ) { $w = (int) ($this->numerator / $this->denominator); - $this->wholeNumbers += $w; + $s = $this->n->sign($this->wholeNumbers); + $s = $s > 0 || $s < 0 ? $s : 1; + $this->wholeNumbers += $s * $w; $this->numerator -= $w * $this->denominator; } return $this; @@ -314,7 +312,17 @@ public function mixed() */ public function int() { - return (int) $this->wholeNumbers; + $w = $this->wholeNumbers; + $n = $this->numerator; + $d = $this->denominator; + if (!$this->n->isInt($n) || !$this->n->isNatural($d)) { + return; + } + $i = $this->mixed()->wholeNumbers; + $this->wholeNumbers = $w; + $this->numerator = $n; + $this->denominator = $d; + return $i; } /** diff --git a/src/Number.php b/src/Number.php index 76405f1..303e738 100644 --- a/src/Number.php +++ b/src/Number.php @@ -28,11 +28,12 @@ public function isInt($n) */ public function isIntAll(array $ns) { - $r = (int) (count($ns) > 0); foreach ($ns as $n) { - $r *= (int) $this->isInt($n); + if (!$this->isInt($n)) { + return false; + } } - return (bool) $r; + return count($ns) > 0; } /** @@ -55,11 +56,12 @@ public function isNatural($n) */ public function isNaturalAll(array $ns) { - $r = (int) (count($ns) > 0); foreach ($ns as $n) { - $r *= (int) $this->isNatural($n); + if (!$this->isNatural($n)) { + return false; + } } - return (bool) $r; + return count($ns) > 0; } /** @@ -79,11 +81,12 @@ public function isFloat($n) */ public function isFloatAll(array $ns) { - $r = (int) (count($ns) > 0); foreach ($ns as $n) { - $r *= (int) $this->isFloat($n); + if (!$this->isFloat($n)) { + return false; + } } - return (bool) $r; + return count($ns) > 0; } /** @@ -103,11 +106,12 @@ public function isNumber($n) */ public function isNumberAll(array $ns) { - $r = (int) (count($ns) > 0); foreach ($ns as $n) { - $r *= (int) $this->isNumber($n); + if (!$this->isNumber($n)) { + return false; + } } - return (bool) $r; + return count($ns) > 0; } /** @@ -130,11 +134,12 @@ public function isFraction($n) */ public function isFractionAll(array $ns) { - $r = (int) (count($ns) > 0); foreach ($ns as $n) { - $r *= (int) $this->isFraction($n); + if (!$this->isFraction($n)) { + return false; + } } - return (bool) $r; + return count($ns) > 0; } /** diff --git a/src/loader.php b/src/loader.php deleted file mode 100644 index 6db09f4..0000000 --- a/src/loader.php +++ /dev/null @@ -1,10 +0,0 @@ - null, 'expect' => 0, ], - ['w' => -10, 'expect' => -10, ], - ['w' => -1, 'expect' => -1, ], - ['w' => 0, 'expect' => 0, ], - ['w' => 1, 'expect' => 1, ], - ['w' => 2, 'expect' => 2, ], - ['w' => 3, 'expect' => 3, ], + ['w' => null, 'n' => null, 'd' => null, 'expect' => null, ], + ['w' => 1, 'n' => null, 'd' => null, 'expect' => null, ], + ['w' => null, 'n' => 1, 'd' => null, 'expect' => null, ], + ['w' => null, 'n' => null, 'd' => 1, 'expect' => null, ], + ['w' => 1, 'n' => null, 'd' => 1, 'expect' => null, ], + ['w' => null, 'n' => 1, 'd' => 1, 'expect' => 1, ], + ['w' => 1, 'n' => 1, 'd' => null, 'expect' => null, ], + ['w' => 1, 'n' => 1, 'd' => 1, 'expect' => 2, ], + ['w' => 1, 'n' => 0, 'd' => 1, 'expect' => 1, ], + ['w' => null, 'n' => 1, 'd' => 2, 'expect' => 0, ], + ['w' => null, 'n' => 3, 'd' => 2, 'expect' => 1, ], + ['w' => -10, 'n' => 1, 'd' => 2, 'expect' => -10, ], + ['w' => -1, 'n' => 1, 'd' => 2, 'expect' => -1, ], + ['w' => 0, 'n' => 1, 'd' => 2, 'expect' => 0, ], + ['w' => 1, 'n' => 2, 'd' => 3, 'expect' => 1, ], + ['w' => 2, 'n' => 3, 'd' => 4, 'expect' => 2, ], + ['w' => 3, 'n' => 4, 'd' => 5, 'expect' => 3, ], + ['w' => -10, 'n' => 5, 'd' => 2, 'expect' => -12, ], + ['w' => -10, 'n' => -5, 'd' => 2, 'expect' => -8, ], + ['w' => -1, 'n' => 3, 'd' => 2, 'expect' => -2, ], + ['w' => -1, 'n' => -3, 'd' => 2, 'expect' => 0, ], + ['w' => -1, 'n' => -5, 'd' => 2, 'expect' => 1, ], ]; $f = new Fraction(); foreach ($cases as $case) { $f->wholeNumbers = $case['w']; + $f->numerator = $case['n']; + $f->denominator = $case['d']; $this->assertSame($case['expect'], $f->int()); } } diff --git a/tests/MultipleTest.php b/tests/MultipleTest.php index a5daad7..2191298 100644 --- a/tests/MultipleTest.php +++ b/tests/MultipleTest.php @@ -5,10 +5,6 @@ namespace Macocci7\PhpMathInteger; require_once('vendor/autoload.php'); -require_once('src/Number.php'); -require_once('src/Prime.php'); -require_once('src/Divisor.php'); -require_once('src/Multiple.php'); use PHPUnit\Framework\TestCase; use Macocci7\PhpMathInteger\Multiple; diff --git a/tests/NumberTest.php b/tests/NumberTest.php index 41c3d48..766d585 100644 --- a/tests/NumberTest.php +++ b/tests/NumberTest.php @@ -5,14 +5,13 @@ namespace Macocci7\PhpMathInteger; require_once('vendor/autoload.php'); -require_once('src/Number.php'); use PHPUnit\Framework\TestCase; use Macocci7\PhpMathInteger\Number; final class NumberTest extends TestCase { - public function test_isInteger_can_judge_correctly(): void + public function test_isInt_can_judge_correctly(): void { $cases = [ ['param' => null, 'expect' => false, ], diff --git a/tests/PrimeTest.php b/tests/PrimeTest.php index cbef4f8..87d3e61 100644 --- a/tests/PrimeTest.php +++ b/tests/PrimeTest.php @@ -5,8 +5,6 @@ namespace Macocci7\PhpMathInteger; require_once('vendor/autoload.php'); -require_once('src/Number.php'); -require_once('src/Prime.php'); use PHPUnit\Framework\TestCase; use Macocci7\PhpMathInteger\Prime;