This PHP-Library can be used to make simple calculations with dynamic number-systems.
Number-System can be installed via. Composer:
composer require "gries/number-system"
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)
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
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
- No support for negative numbers
- No support of floating point numbers
- Division always rounds to 0 because ... no floating point number support ;)
vendor/bin/phpspec run
Feel free to give me feedback/feature-request/bug-reports via. github issues. Or just send me a pull-request :)
For the full copyright and license information, please view the LICENSE file that was distributed with this source code.