{"id":20062738,"url":"https://github.com/anizoptera/math","last_synced_at":"2025-05-05T17:31:47.616Z","repository":{"id":6770317,"uuid":"8017114","full_name":"Anizoptera/Math","owner":"Anizoptera","description":"AzaMath - Anizoptera CMF mathematic component. Provides functionality to work with large numbers with arbitrary precision (using BCMath) and universal mumeral systems conversions (supported bases from 2 to 62 inclusive, and custom systems; pure PHP, but can use GMP and core functions for speed optimization).","archived":false,"fork":false,"pushed_at":"2017-02-02T22:35:18.000Z","size":61,"stargazers_count":19,"open_issues_count":0,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-09-24T20:43:16.595Z","etag":null,"topics":["anizoptera-cmf","arithmetic","bcmath","composer","custom-numeral","gmp","math","numeral-system","numeral-systems-conversions","packagist","php","php-library","positional-numeral-systems","travis"],"latest_commit_sha":null,"homepage":"https://github.com/Anizoptera/Math","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Anizoptera.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-02-04T21:50:32.000Z","updated_at":"2024-05-21T13:25:14.000Z","dependencies_parsed_at":"2022-08-26T05:51:52.564Z","dependency_job_id":null,"html_url":"https://github.com/Anizoptera/Math","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anizoptera%2FMath","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anizoptera%2FMath/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anizoptera%2FMath/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anizoptera%2FMath/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Anizoptera","download_url":"https://codeload.github.com/Anizoptera/Math/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224457023,"owners_count":17314410,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["anizoptera-cmf","arithmetic","bcmath","composer","custom-numeral","gmp","math","numeral-system","numeral-systems-conversions","packagist","php","php-library","positional-numeral-systems","travis"],"created_at":"2024-11-13T13:38:39.963Z","updated_at":"2024-11-13T13:38:40.851Z","avatar_url":"https://github.com/Anizoptera.png","language":"PHP","readme":"AzaMath\n=======\n\nAnizoptera CMF mathematic component.\n\nhttps://github.com/Anizoptera/Math\n\n[![Build Status][TravisImage]][Travis]\n\n\nTable of Contents\n-----------------\n\n1. [Introduction](#introduction)\n2. [Requirements](#requirements)\n3. [Installation](#installation)\n4. [Examples](#examples)\n   * [Numeral systems conversions](#example-1---numeral-systems-conversions)\n   * [Custom numeral system](#example-2---custom-numeral-system)\n   * [Arbitrary precision arithmetic](#example-3---arbitrary-precision-arithmetic)\n   * [Input filtration](#example-4---input-filtration)\n   * [Do some operations and then convert to base62](#example-5---do-some-operations-and-then-convert-to-base62)\n5. [Tests](#tests)\n6. [Credits](#credits)\n7. [License](#license)\n8. [Links](#links)\n\n\nIntroduction\n------------\n\nProvides functionality to work with large numbers with arbitrary precision (using BCMath).\nand universal convertor between positional numeral systems (supported bases from 2 to 62 inclusive, and custom systems; pure PHP realisation, but can use GMP and core PHP functions for speed optimization).\n\nFeatures:\n\n* Functionality to work with large numbers (integers, floats) with arbitrary precision (requires [BCMath][]). Can work with floats (E notation too!) and without loosing precision (as far as possible). It supports all basic arithmetic, exponentiation, square root, modulus, bit shift, rounding, comparison, and some other operations.\n* Very simple math for big integers (only integers!, has a native PHP realization and can use BCMath or GMP for speedup).\n* Universal number (and huge number!) convertor between positional numeral systems (supported bases from 2 to 62 inclusive, and systems with custom alphabet; pure PHP realisation, but can use [GMP][] and [core PHP](http://php.net/math) functions for speed optimization). Negative and huge integers are supported.\n* Convenient, fully documented and test covered API.\n\n\nRequirements\n------------\n\n* PHP 5.3.3 (or later);\n* [BCMath (Binary Calculator Arbitrary Precision Mathematics)][BCMath] - Required only to work with arbitrary precision arithmetic operations;\n* [GMP (GNU Multiple Precision)][GMP] - Recommended. Used to speed up number systems conversions and arbitrary precision arithmetic operations;\n\n\nInstallation\n------------\n\nThe recommended way to install AzaMath is [through composer](http://getcomposer.org).\nYou can see [package information on Packagist][ComposerPackage].\n\n```JSON\n{\n    \"require\": {\n        \"aza/math\": \"~1.0\"\n    }\n}\n```\n\n\nExamples\n--------\n\nYou can use [examples/example.php](examples/example.php) to run all examples.\n\n#### Example #1 - Numeral systems conversions\n\n```php\n$res = NumeralSystem::convert('WIKIPEDIA', 36, 10);\necho $res . PHP_EOL; // 91730738691298\n\n$res = NumeralSystem::convert('9173073869129891730738691298', 10, 16);\necho $res . PHP_EOL; // 1da3c9f2dd3133d4ed04bce2\n\n$res = NumeralSystem::convertTo('9173073869129891730738691298', 62);\necho $res . PHP_EOL; // BvepB3yk4UBFhGew\n\n$res = NumeralSystem::convertFrom('BvepB3yk4UBFhGew', 62);\necho $res . PHP_EOL; // 9173073869129891730738691298\n```\n\n#### Example #2 - Custom numeral system\n\n```php\n// Add new system with custom alphabet\n// Each char must appear only once.\n// It should use only one byte characters.\n$alphabet = '!@#$%^\u0026*()_+=-'; // base 14 equivalent\n$system   = 'StrangeSystem';\nNumeralSystem::setSystem($system, $alphabet);\n\n$number = '9999';\n$res = NumeralSystem::convertTo($number, $system);\necho $res . PHP_EOL; // $)!$\n\n$res = NumeralSystem::convertFrom($res, $system);\necho $res . PHP_EOL; // 9999\n\n\n// Full binary alphabet\n$system = 'binary';\nNumeralSystem::setSystem($system, $alphabet);\n// Examples with it\n$var = 'example';\n$expected_hex = ltrim(sha1($var), '0'); // sha1 can return hex value padded with zeros\n$expected_bin = sha1($var, true);       // raw sha1 hash (binary representation)\n$result_hex   = NumeralSystem::convert($expected_bin, $system, 16);\n$result_bin   = NumeralSystem::convert($expected_hex, 16, $system);\necho $expected_hex . PHP_EOL; // c3499c2729730a7f807efb8676a92dcb6f8a3f8f\necho $result_hex . PHP_EOL;   // c3499c2729730a7f807efb8676a92dcb6f8a3f8f\necho ($expected_bin === $result_bin) . PHP_EOL; // 1\n```\n\n#### Example #3 - Arbitrary precision arithmetic\n\n```php\n// Create new big number with the specified precision for operations - 20 (default is 100)\n$number = new BigNumber('118059162071741130342591466421', 20);\n\n// Divide number\n$number-\u003edivide(12345678910);\necho $number . PHP_EOL; // 9562792207086578954.49764831288650451382\n\n// Divide again and round with the specified precision and algorithm\n// Three round algorithms a supported: HALF_UP, HALF_DOWN, CUT.\n// You can use them as BigNumber::ROUND_* or PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN.\n// Default is HALF_UP.\n$number-\u003edivide(9876543210)-\u003eround(3, PHP_ROUND_HALF_DOWN);\necho $number . PHP_EOL; // 968232710.955\n\n// Comparisions\n$number = new BigNumber(10);\necho ($number-\u003ecompareTo(20) \u003c 0) . PHP_EOL; // 1\necho $number-\u003eisLessThan(20) . PHP_EOL; // 1\n\n$number = new BigNumber(20);\necho ($number-\u003ecompareTo(10) \u003e 0) . PHP_EOL; // 1\necho $number-\u003eisGreaterThan(10) . PHP_EOL; // 1\n\n$number = new BigNumber(20);\necho ($number-\u003ecompareTo(20) === 0) . PHP_EOL; // 1\necho $number-\u003eisLessThanOrEqualTo(20) . PHP_EOL; // 1\n```\n\n#### Example #4 - Input filtration\n\n```php\n// The arguments of all functions are also filtered.\n$number = new BigNumber(\"9,223 372`036'854,775.808000\");\necho $number . PHP_EOL; // 9223372036854775.808\n```\n\n#### Example #5 - Do some operations and then convert to base62\n\n```php\n$number = new BigNumber('9223372036854775807');\n$number = $number-\u003epow(2)-\u003econvertToBase(62);\necho $number . PHP_EOL; // 1wlVYJaWMuw53lV7Cg98qn\n```\n\n\nTests\n-----\n\nTests are in the `Tests` folder and reach 100% code-coverage.\nTo run them, you need PHPUnit.\nExample:\n\n    $ phpunit --configuration phpunit.xml.dist\n\nOr with coverage report:\n\n    $ phpunit --configuration phpunit.xml.dist --coverage-html code_coverage/\n\n\nCredits\n-------\n\nAzaMath is a part of [Anizoptera CMF][], written by [Amal Samally][] (amal.samally at gmail.com) and [AzaGroup][] team.\u003cbr/\u003e\nArbitrary precision arithmetic part is partially based on [Moontoast Math Library](https://github.com/moontoast/math).\n\n\nLicense\n-------\n\nReleased under the [MIT](LICENSE.md) license.\n\n\nLinks\n-----\n\n* [Composer package][ComposerPackage]\n* [Last build on the Travis CI][Travis]\n* [Project profile on the Ohloh](https://www.ohloh.net/p/AzaMath)\n* (RU) [AzaMath — Cистемы счисления (включая кастомные) + арифметика произвольной точности на PHP](http://habrahabr.ru/post/168935/)\n* Other Anizoptera CMF components on the [GitHub][Anizoptera CMF] / [Packagist](https://packagist.org/packages/aza)\n* (RU) [AzaGroup team blog][AzaGroup]\n\n\n\n[BCMath]: http://php.net/bcmath\n[GMP]:    http://php.net/gmp\n\n[Anizoptera CMF]:  https://github.com/Anizoptera\n[Amal Samally]:    http://azagroup.ru/about/#amal\n[AzaGroup]:        http://azagroup.ru/\n[ComposerPackage]: https://packagist.org/packages/aza/math\n[TravisImage]:     https://secure.travis-ci.org/Anizoptera/Math.png?branch=master\n[Travis]:          https://travis-ci.org/Anizoptera/Math\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanizoptera%2Fmath","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanizoptera%2Fmath","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanizoptera%2Fmath/lists"}