Skip to content

Commit 59846bd

Browse files
authored
Merge pull request #22 from macocci7/dev_#19
#19: #20: #21: all of these issues are reflected.
2 parents 8f6ca61 + 9dba8db commit 59846bd

11 files changed

Lines changed: 62 additions & 66 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "macocci7/php-math-integer",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "PHP Math Library of the subjects of number theory(only natural number).",
55
"type": "library",
66
"license": "MIT",

src/Fraction.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,15 +293,13 @@ public function improper()
293293
public function mixed()
294294
{
295295
if (
296-
$this->n->isNaturalAll(
297-
[
298-
$this->numerator,
299-
$this->denominator,
300-
]
301-
)
296+
$this->n->isInt($this->numerator)
297+
&& $this->n->isNatural($this->denominator)
302298
) {
303299
$w = (int) ($this->numerator / $this->denominator);
304-
$this->wholeNumbers += $w;
300+
$s = $this->n->sign($this->wholeNumbers);
301+
$s = $s > 0 || $s < 0 ? $s : 1;
302+
$this->wholeNumbers += $s * $w;
305303
$this->numerator -= $w * $this->denominator;
306304
}
307305
return $this;
@@ -314,7 +312,17 @@ public function mixed()
314312
*/
315313
public function int()
316314
{
317-
return (int) $this->wholeNumbers;
315+
$w = $this->wholeNumbers;
316+
$n = $this->numerator;
317+
$d = $this->denominator;
318+
if (!$this->n->isInt($n) || !$this->n->isNatural($d)) {
319+
return;
320+
}
321+
$i = $this->mixed()->wholeNumbers;
322+
$this->wholeNumbers = $w;
323+
$this->numerator = $n;
324+
$this->denominator = $d;
325+
return $i;
318326
}
319327

320328
/**

src/Number.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ public function isInt($n)
2828
*/
2929
public function isIntAll(array $ns)
3030
{
31-
$r = (int) (count($ns) > 0);
3231
foreach ($ns as $n) {
33-
$r *= (int) $this->isInt($n);
32+
if (!$this->isInt($n)) {
33+
return false;
34+
}
3435
}
35-
return (bool) $r;
36+
return count($ns) > 0;
3637
}
3738

3839
/**
@@ -55,11 +56,12 @@ public function isNatural($n)
5556
*/
5657
public function isNaturalAll(array $ns)
5758
{
58-
$r = (int) (count($ns) > 0);
5959
foreach ($ns as $n) {
60-
$r *= (int) $this->isNatural($n);
60+
if (!$this->isNatural($n)) {
61+
return false;
62+
}
6163
}
62-
return (bool) $r;
64+
return count($ns) > 0;
6365
}
6466

6567
/**
@@ -79,11 +81,12 @@ public function isFloat($n)
7981
*/
8082
public function isFloatAll(array $ns)
8183
{
82-
$r = (int) (count($ns) > 0);
8384
foreach ($ns as $n) {
84-
$r *= (int) $this->isFloat($n);
85+
if (!$this->isFloat($n)) {
86+
return false;
87+
}
8588
}
86-
return (bool) $r;
89+
return count($ns) > 0;
8790
}
8891

8992
/**
@@ -103,11 +106,12 @@ public function isNumber($n)
103106
*/
104107
public function isNumberAll(array $ns)
105108
{
106-
$r = (int) (count($ns) > 0);
107109
foreach ($ns as $n) {
108-
$r *= (int) $this->isNumber($n);
110+
if (!$this->isNumber($n)) {
111+
return false;
112+
}
109113
}
110-
return (bool) $r;
114+
return count($ns) > 0;
111115
}
112116

113117
/**
@@ -130,11 +134,12 @@ public function isFraction($n)
130134
*/
131135
public function isFractionAll(array $ns)
132136
{
133-
$r = (int) (count($ns) > 0);
134137
foreach ($ns as $n) {
135-
$r *= (int) $this->isFraction($n);
138+
if (!$this->isFraction($n)) {
139+
return false;
140+
}
136141
}
137-
return (bool) $r;
142+
return count($ns) > 0;
138143
}
139144

140145
/**

src/loader.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/BezoutTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
namespace Macocci7\PhpMathInteger;
66

77
require_once('vendor/autoload.php');
8-
require_once('src/Number.php');
9-
require_once('src/Prime.php');
10-
require_once('src/Divisor.php');
11-
require_once('src/Euclid.php');
12-
require_once('src/Bezout.php');
138

149
use PHPUnit\Framework\TestCase;
1510
use Macocci7\PhpMathInteger\Bezout;

tests/DivisorTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
namespace Macocci7\PhpMathInteger;
66

77
require_once('vendor/autoload.php');
8-
require_once('src/Number.php');
9-
require_once('src/Prime.php');
10-
require_once('src/Divisor.php');
118

129
use PHPUnit\Framework\TestCase;
1310
use Macocci7\PhpMathInteger\Divisor;

tests/EuclidTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
namespace Macocci7\PhpMathInteger;
66

77
require_once('vendor/autoload.php');
8-
require_once('src/Number.php');
9-
require_once('src/Prime.php');
10-
require_once('src/Euclid.php');
118

129
use PHPUnit\Framework\TestCase;
1310
use Macocci7\PhpMathInteger\Euclid;

tests/FractionTest.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
namespace Macocci7\PhpMathInteger;
66

77
require_once('vendor/autoload.php');
8-
require_once('src/Number.php');
9-
require_once('src/Prime.php');
10-
require_once('src/Divisor.php');
11-
require_once('src/Euclid.php');
12-
require_once('src/Multiple.php');
13-
require_once('src/Fraction.php');
148

159
use PHPUnit\Framework\TestCase;
1610
use Macocci7\PhpMathInteger\Fraction;
@@ -634,17 +628,34 @@ public function test_mixed_can_make_mixed_fraction_correctly(): void
634628
public function test_int_can_return_integer_correctly(): void
635629
{
636630
$cases = [
637-
['w' => null, 'expect' => 0, ],
638-
['w' => -10, 'expect' => -10, ],
639-
['w' => -1, 'expect' => -1, ],
640-
['w' => 0, 'expect' => 0, ],
641-
['w' => 1, 'expect' => 1, ],
642-
['w' => 2, 'expect' => 2, ],
643-
['w' => 3, 'expect' => 3, ],
631+
['w' => null, 'n' => null, 'd' => null, 'expect' => null, ],
632+
['w' => 1, 'n' => null, 'd' => null, 'expect' => null, ],
633+
['w' => null, 'n' => 1, 'd' => null, 'expect' => null, ],
634+
['w' => null, 'n' => null, 'd' => 1, 'expect' => null, ],
635+
['w' => 1, 'n' => null, 'd' => 1, 'expect' => null, ],
636+
['w' => null, 'n' => 1, 'd' => 1, 'expect' => 1, ],
637+
['w' => 1, 'n' => 1, 'd' => null, 'expect' => null, ],
638+
['w' => 1, 'n' => 1, 'd' => 1, 'expect' => 2, ],
639+
['w' => 1, 'n' => 0, 'd' => 1, 'expect' => 1, ],
640+
['w' => null, 'n' => 1, 'd' => 2, 'expect' => 0, ],
641+
['w' => null, 'n' => 3, 'd' => 2, 'expect' => 1, ],
642+
['w' => -10, 'n' => 1, 'd' => 2, 'expect' => -10, ],
643+
['w' => -1, 'n' => 1, 'd' => 2, 'expect' => -1, ],
644+
['w' => 0, 'n' => 1, 'd' => 2, 'expect' => 0, ],
645+
['w' => 1, 'n' => 2, 'd' => 3, 'expect' => 1, ],
646+
['w' => 2, 'n' => 3, 'd' => 4, 'expect' => 2, ],
647+
['w' => 3, 'n' => 4, 'd' => 5, 'expect' => 3, ],
648+
['w' => -10, 'n' => 5, 'd' => 2, 'expect' => -12, ],
649+
['w' => -10, 'n' => -5, 'd' => 2, 'expect' => -8, ],
650+
['w' => -1, 'n' => 3, 'd' => 2, 'expect' => -2, ],
651+
['w' => -1, 'n' => -3, 'd' => 2, 'expect' => 0, ],
652+
['w' => -1, 'n' => -5, 'd' => 2, 'expect' => 1, ],
644653
];
645654
$f = new Fraction();
646655
foreach ($cases as $case) {
647656
$f->wholeNumbers = $case['w'];
657+
$f->numerator = $case['n'];
658+
$f->denominator = $case['d'];
648659
$this->assertSame($case['expect'], $f->int());
649660
}
650661
}

tests/MultipleTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
namespace Macocci7\PhpMathInteger;
66

77
require_once('vendor/autoload.php');
8-
require_once('src/Number.php');
9-
require_once('src/Prime.php');
10-
require_once('src/Divisor.php');
11-
require_once('src/Multiple.php');
128

139
use PHPUnit\Framework\TestCase;
1410
use Macocci7\PhpMathInteger\Multiple;

tests/NumberTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
namespace Macocci7\PhpMathInteger;
66

77
require_once('vendor/autoload.php');
8-
require_once('src/Number.php');
98

109
use PHPUnit\Framework\TestCase;
1110
use Macocci7\PhpMathInteger\Number;
1211

1312
final class NumberTest extends TestCase
1413
{
15-
public function test_isInteger_can_judge_correctly(): void
14+
public function test_isInt_can_judge_correctly(): void
1615
{
1716
$cases = [
1817
['param' => null, 'expect' => false, ],

0 commit comments

Comments
 (0)