Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SakiTakamachi committed Dec 4, 2024
0 parents commit e66c56f
Show file tree
Hide file tree
Showing 37 changed files with 1,712 additions and 0 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Push

on:
- pull_request
- push

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.url || github.run_id }}
cancel-in-progress: true

jobs:
Linux-64:
name: Tests on Linux 64-bit

runs-on: ubuntu-24.04

strategy:
fail-fast: false
matrix:
php-version:
- '8.4'
- '8.5'

steps:
- name: git checkout
uses: actions/checkout@v4

- name: Install PHP with extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: bcmath
ini-values: assert.exception=1, zend.assertions=1, error_reporting=-1, log_errors_max_len=0, display_errors=On
tools: none

- name: composer install
run: composer install -n --prefer-dist

- name: Run tests
if: matrix.php-version == '8.5'
run: |
./vendor/bin/phpunit \
--configuration phpunit.xml.dist \
--testsuite=unit
- name: Run tests with coverage
if: matrix.php-version == '8.4'
run: |
XDEBUG_MODE=coverage ./vendor/bin/phpunit \
--configuration phpunit.xml.dist \
--testsuite=unit \
--coverage-clover ./coverage.xml
- uses: codecov/codecov-action@v5
if: matrix.php-version == '8.4'
with:
files: ./coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}
verbose: true
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Saki Takamachi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Fraction

[![Push](https://github.com/SakiTakamachi/php-fraction/actions/workflows/push.yml/badge.svg)](https://github.com/SakiTakamachi/php-fraction/actions/workflows/push.yml)
[![codecov](https://codecov.io/gh/SakiTakamachi/php-fraction/graph/badge.svg?token=4MIM2LRPRD)](https://codecov.io/gh/SakiTakamachi/php-fraction)

## Install

```
composer require saki/fraction
```

This library requires PHP 8.4 or higher and the BCMath extension.

## Description

This is a PHP library that handles fractions using `BcMath\Number`.
The class is `final` and `readonly`, so an immutable object.

### Create a object

The constructor is:

```
// __construct(Number|int|string $numerator, Number|int|string $denominator = 1)
use Saki\Fraction;
$fraction = new Fraction(1, 2); // 1/2
$fraction = new Fraction('0.5', -1); // -1/2
$fraction = new Fraction('-3'); // -3/1
$fraction = new Fraction('-2', '-4'); // 1/2
```

You can also create objects from strings representing fractions like `"1/2"`.

```
$fraction = Saki\Fraction::createFromFractionString('1/2');
```

### Methods

The available methods are:

- `add(Fraction|Number|int|string $num): Fraction`
- `sub(Fraction|Number|int|string $num): Fraction`
- `mul(Fraction|Number|int|string $num): Fraction`
- `div(Fraction|Number|int|string $num): Fraction`
- `mod(Fraction|Number|int|string $num): Fraction`
- `divmod(Fraction|Number|int|string $num): Fraction[]`
- `pow(Number|int|string $num): Fraction`
- `compare(Fraction|Number|int|string $num): int`
- `toNumber(): Number`

Additionally, this class is `Stringable` and supports serialization.

### Convert to `BcMath\Number`

When converting a `Fraction` to `BcMath\Number`, for example, a value like `"1/10000000000"` was too small and could end up as `"0"`.
Therefore, in this case, the numerator is `1` digit and the denominator is `11` digits, so the division is executed with `BcMath\Number::scale` extended by the `10` digits of the difference.
Division of `BcMath\Number` automatically extends `BcMath\Number::scale` by up to another `10` digits from this state, so you can obtain sufficient precision.

e.g.

```
// Example of indivisible division
use Saki\Fraction;
$fraction = new Fraction(1, 30000);
var_dump($fraction->toNumber());
```

output:

```
object(BcMath\Number)#2 (2) {
["value"]=>
string(16) "0.00003333333333"
["scale"]=>
int(14)
}
```

### Cast to `string`

When cast to `string`, it becomes a `string` representing a fraction, such as `"-3/10"`.
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "saki/fraction",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Saki\\": "src/"
}
},
"authors": [
{
"name": "Saki Takamachi",
"email": "[email protected]"
}
],
"require": {
"php": ">=8.4",
"ext-bcmath": "*"
},
"require-dev": {
"phpunit/phpunit": "^11.4"
}
}
13 changes: 13 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="unit">
<directory suffix=".phpt">tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
Loading

0 comments on commit e66c56f

Please sign in to comment.