gries / number-system
A php library to deal with dynamic number systems and conversion
dev-master
2015-01-10 18:12 UTC
Requires
- ext-gmp: *
Requires (Dev)
- phpspec/phpspec: ~2.1
This package is auto-updated.
Last update: 2024-10-16 21:32:51 UTC
README
This PHP-Library can be used to make simple calculations with dynamic number-systems.
Installation
Number-System can be installed via. Composer:
composer require "gries/number-system"
Usage
Simple
use gries\NumberSystem\HexadecimalSystem; use gries\NumberSystem\Number; require_once __DIR__.'/../vendor/autoload.php'; $number = new Number(15); // the default system is decimal $hexNumber = new Number('FF', new HexadecimalSystem()); echo $number->add($hexNumber)->value(); // 270 (15 + 255) echo $hexNumber->add($number)->value(); // 10E (F + FF)
Custom Number System
use gries\NumberSystem\Number; use gries\NumberSystem\NumberSystem; require_once __DIR__.'/../vendor/autoload.php'; $elementalSystem = new NumberSystem(['earth', 'fire', 'air', 'water'], '-'); // diffrent ways of creating a number $number = new Number('fire-earth', $elementalSystem); // 4 $largerNumber = $elementalSystem->buildNumber(['fire', 'air', 'air']); // 26 // define your own number system and number class YoloSystem extends NumberSystem { public function __construct() { parent::__construct(['1337', 'yolo', 'swag'], '¿'); } } class YoloNumber extends Number { public function __construct($value) { parent::__construct($value, new YoloSystem()); } } $foo = new YoloNumber('yolo¿swag¿yolo¿yolo¿yolo¿yolo¿swag'); echo $foo->asDecimalString(); // 1337
Converting Mathematical Expressions
You can use the ExpressionConverter
to convert mathematical expressions from one system to another.
$decimal = new NumberSystem(); $binary = new BinaryNumberSystem(); $converter = new ExpressionConverter(); $decimalExpression = new Expression('(1337 * 7) + sin(5)-2', $decimal); echo $converter->convert($decimalExpression, $binary); // -> (10100111001 * 111) + sin(101)-10
Current limitations aka todo's
- No support for negative numbers
- No support of floating point numbers
- Division always rounds to 0 because ... no floating point number support ;)
Running the tests
vendor/bin/phpspec run
Contribute!
Feel free to give me feedback/feature-request/bug-reports via. github issues. Or just send me a pull-request :)
Author
License
For the full copyright and license information, please view the LICENSE file that was distributed with this source code.