Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
24 changes: 16 additions & 8 deletions src/Fraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down
35 changes: 20 additions & 15 deletions src/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down
10 changes: 0 additions & 10 deletions src/loader.php

This file was deleted.

5 changes: 0 additions & 5 deletions tests/BezoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +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/Euclid.php');
require_once('src/Bezout.php');

use PHPUnit\Framework\TestCase;
use Macocci7\PhpMathInteger\Bezout;
Expand Down
3 changes: 0 additions & 3 deletions tests/DivisorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +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');

use PHPUnit\Framework\TestCase;
use Macocci7\PhpMathInteger\Divisor;
Expand Down
3 changes: 0 additions & 3 deletions tests/EuclidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
namespace Macocci7\PhpMathInteger;

require_once('vendor/autoload.php');
require_once('src/Number.php');
require_once('src/Prime.php');
require_once('src/Euclid.php');

use PHPUnit\Framework\TestCase;
use Macocci7\PhpMathInteger\Euclid;
Expand Down
37 changes: 24 additions & 13 deletions tests/FractionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +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/Euclid.php');
require_once('src/Multiple.php');
require_once('src/Fraction.php');

use PHPUnit\Framework\TestCase;
use Macocci7\PhpMathInteger\Fraction;
Expand Down Expand Up @@ -634,17 +628,34 @@ public function test_mixed_can_make_mixed_fraction_correctly(): void
public function test_int_can_return_integer_correctly(): void
{
$cases = [
['w' => 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());
}
}
Expand Down
4 changes: 0 additions & 4 deletions tests/MultipleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions tests/NumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, ],
Expand Down
2 changes: 0 additions & 2 deletions tests/PrimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down