{"id":22509241,"url":"https://github.com/mathematicator-core/numbers","last_synced_at":"2025-08-03T13:31:23.727Z","repository":{"id":35034121,"uuid":"198896282","full_name":"mathematicator-core/numbers","owner":"mathematicator-core","description":"Fast \u0026 secure storage for numbers with arbitrary precision. It supports Human string and LaTeX output and basic conversions.","archived":false,"fork":false,"pushed_at":"2023-02-03T04:02:55.000Z","size":207,"stargazers_count":2,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-06T10:38:54.146Z","etag":null,"topics":["fraction","math","number-theory","numbers","precision"],"latest_commit_sha":null,"homepage":"https://mathematicator.com","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mathematicator-core.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/funding.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"janbarasek","custom":["https://baraja.cz","https://php.baraja.cz","https://vue.baraja.cz"]}},"created_at":"2019-07-25T20:21:01.000Z","updated_at":"2022-06-20T06:20:34.000Z","dependencies_parsed_at":"2023-02-18T03:00:16.702Z","dependency_job_id":null,"html_url":"https://github.com/mathematicator-core/numbers","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathematicator-core%2Fnumbers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathematicator-core%2Fnumbers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathematicator-core%2Fnumbers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathematicator-core%2Fnumbers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mathematicator-core","download_url":"https://codeload.github.com/mathematicator-core/numbers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228547984,"owners_count":17935156,"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":["fraction","math","number-theory","numbers","precision"],"created_at":"2024-12-07T01:28:15.664Z","updated_at":"2024-12-07T01:28:16.295Z","avatar_url":"https://github.com/mathematicator-core.png","language":"PHP","funding_links":["https://github.com/sponsors/janbarasek","https://baraja.cz","https://php.baraja.cz","https://vue.baraja.cz"],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n    Smart PHP Number Utilities\n\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://mathematicator.com\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://avatars3.githubusercontent.com/u/44620375?s=100\u0026v=4\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n[![Integrity check](https://github.com/mathematicator-core/numbers/workflows/Integrity%20check/badge.svg)](https://github.com/mathematicator-core/numbers/actions?query=workflow%3A%22Integrity+check%22)\n[![codecov](https://codecov.io/gh/mathematicator-core/numbers/branch/master/graph/badge.svg)](https://codecov.io/gh/mathematicator-core/numbers)\n[![Latest Stable Version](https://poser.pugx.org/mathematicator-core/numbers/v/stable)](https://packagist.org/packages/mathematicator-core/numbers)\n[![Latest Unstable Version](https://poser.pugx.org/mathematicator-core/numbers/v/unstable)](https://packagist.org/packages/mathematicator-core/numbers)\n[![License: MIT](https://img.shields.io/badge/License-MIT-brightgreen.svg)](./LICENSE)\n[![PHPStan Enabled](https://img.shields.io/badge/PHPStan-enabled%20L8-brightgreen.svg?style=flat)](https://phpstan.org/)\n\n**A PHP library to safely store and represent numbers and its equivalents in PHP.**\n\nStore lots of number types exactly (**integers, decimals, fractions**) and convert\nthem to each other. Expressions can be outputted as a **human string** (e.g. `1/2`)\nor **LaTeX** (e.g. `\\frac{1}{2}`).\n\nIt is highly recommended to make sure you have enabled [BCMath](https://www.php.net/manual/en/book.bc.php)\nor [GMP](https://www.php.net/manual/en/book.gmp.php) extension on your PHP server for much\nfaster calculations.\n\n## Installation\n\n```bash\ncomposer require mathematicator-core/numbers\n```\n\n## Features\n\n- **SmartNumber** - Unified safe storage for all number types with\n    an arbitrary precision. It supports comparisons.\n    - **Entity\\Number** Less magic universal arbitrary precision\n    number storage with basic features.\n- **Fractions:**\n    - **Entity\\Fraction** - Storage for simple or compound fraction that\n    can consist from numbers and other expressions.\n    - **Entity\\FractionNumbersOnly** - Storage for simple or compound fraction\n    that consists only from numbers and is directly computable.\n- **LaTeX support:** ([What is LaTeX?](https://en.wikipedia.org/wiki/LaTeX))\n    - **MathLatexBuilder** - Create valid LaTeX for mathematical expressions\n    in simple way with a fluent interface.\n    - **MathLatexToolkit** - Statical library for LaTeX expressions\n    (includes constants, functions, operators etc.)\n    - **MathLatexSnippet** - Storage for LaTeX syntax.\n- **Human string support:**\n    - **MathHumanStringBuilder** - same interface as MathLatexBuilder,\n    but produces human strings\n    - **MathHumanStringToolkit** - same interface as MathLatexToolkit,\n    but produces human strings (e.g. `1/2*(3+1)`)\n- **Set generators**\n    - Primary numbers\n    - Even numbers\n    - Odd numbers\n- **Converters:**\n    - Array to fraction and back\n    - Decimal to fraction\n    - Fraction to human string\n    - Fraction to LaTeX\n    - Int to Roman and back\n- **Calculation** - simple arithmetic operations ([brick/math](https://github.com/brick/math) decorator)\n\n💡 **TIP:** You can use [mathematicator-core/tokenizer](https://github.com/mathematicator-core/tokenizer)\nfor advance user input string **tokenization** or [mathematicator-core/calculator](https://github.com/mathematicator-core/calculator)\nfor advance **calculations**.\n\n## Usage\n\n```php\nuse Brick\\Math\\RoundingMode;\nuse Mathematicator\\Numbers\\SmartNumber;\n\n$smartNumber = SmartNumber::of('80.500');\necho $smartNumber-\u003etoBigDecimal(); // 80.500\necho $smartNumber-\u003etoFraction()-\u003egetNumerator(); // 161\necho $smartNumber-\u003etoFraction()-\u003egetDenominator(); // 2\necho Calculation::of($smartNumber)-\u003emultipliedBy(-4); // -322.000\necho Calculation::of($smartNumber)-\u003emultipliedBy(-4)-\u003eabs()-\u003egetResult()-\u003etoInt(); // 322\necho $smartNumber-\u003etoBigDecimal()-\u003etoScale(0, RoundingMode::HALF_UP); // 81\n\n$smartNumber2 = SmartNumber::of('161/2');\necho $smartNumber2-\u003etoHumanString(); // 161/2\necho $smartNumber2-\u003etoHumanString()-\u003eplus(5)-\u003eequals('90.5'); // 161/2+10=90.5\necho $smartNumber2-\u003etoLatex(); // \\frac{161}{2}\necho $smartNumber2-\u003etoBigDecimal();  // 80.5\n```\n\n## Recommended libraries\n\nFor safe operations with arbitrary length numbers we recommend to use:\n\n- [brick/math](https://github.com/brick/math) - Arbitrary precision\narithmetic library for PHP with a simple interface.\n- [PHP BC Math extension](https://www.php.net/manual/en/ref.bc.php) - Native PHP extension for\narbitrary precision computing.\n\n### Working with money\n\nUse one of these libraries if you work with money in your application.\n\n- [brick/money](https://github.com/brick/money) - A money and currency library\nwith an interface like brick/math.\n- [moneyphp/money](https://github.com/moneyphp/money) - Widely adopted PHP\nimplementation of Fowler's Money pattern.\n\n## Why float is not safe?\n\n**Float stores your number as an approximation with limited precision.**\n\nYou should never trust float to the last digit. Do not use floats\ndirectly for checking equity if you rely on precision\n(not only monetary calculations).\n\n**Example:**\n```php\n$result = 0.1 + 0.2;\necho $result; // output: 0.3\n\necho ($result == 0.3) ? 'true' : 'false'; // output: false\n```\n\n[How is float stored in memory?](https://softwareengineering.stackexchange.com/a/215126/354697)\n\n[See in PHP manual](https://www.php.net/manual/en/language.types.float.php)\n\n[Read more about float on Wikipedia](https://en.wikipedia.org/wiki/Floating-point_arithmetic)\n\n## Mathematicator Framework tools structure\n\nThe biggest advantage is that you can choose which layer best fits\nyour needs and start build on the top of it, immediately, without the need\nto create everything by yourself. Our tools are tested for bugs\nand tuned for performance, so you can save a significant amount\nof your time, money, and effort.\n\nFramework tend to be modular as much as possible, so you should be able\nto create an extension on each layer and its sublayers.\n\n**Mathematicator framework layers** ordered from the most concrete\none to the most abstract one:\n\n\u003ctable\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\n            \u003cb\u003e\n            \u003ca href=\"https://github.com/mathematicator-core/search\"\u003e\n                Search\n            \u003c/a\u003e\n            \u003c/b\u003e\n        \u003c/td\u003e\n        \u003ctd\u003e\n            Modular search engine layer that calls its sublayers\n            and creates user interface.\n        \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\n            \u003cb\u003e\n            \u003ca href=\"https://github.com/mathematicator-core/vizualizator\"\u003e\n                Vizualizator\n            \u003c/a\u003e\n            \u003c/b\u003e\n        \u003c/td\u003e\n        \u003ctd\u003e\n            Elegant graphic visualizer that can render to\n            SVG, PNG, JPG and Base64.\u003cbr /\u003e\n            \u003cu\u003eExtensions:\u003c/u\u003e\n            \u003cb\u003e\n            \u003ca href=\"https://github.com/mathematicator-core/mandelbrot-set\"\u003e\n                Mandelbrot set generator\n            \u003c/a\u003e\n            \u003c/b\u003e\n        \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\n            \u003cb\u003e\n            \u003ca href=\"https://github.com/mathematicator-core/calculator\"\u003e\n                Calculator\n            \u003c/a\u003e\n            \u003c/b\u003e\n        \u003c/td\u003e\n        \u003ctd\u003e\n            Modular advance calculations layer.\n            \u003cbr /\u003e\n            \u003cu\u003eExtensions:\u003c/u\u003e\n            \u003cb\u003e\n            \u003ca href=\"https://github.com/mathematicator-core/integral-solver\"\u003e\n                Integral Solver\n            \u003c/a\u003e,\n            \u003ca href=\"https://github.com/mathematicator-core/statistic\"\u003e\n                Statistics\n            \u003c/a\u003e\n            \u003c/b\u003e\n        \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\n            \u003cb\u003e\n            \u003ca href=\"https://github.com/mathematicator-core/engine\"\u003e\n                Engine\n            \u003c/a\u003e\n            \u003c/b\u003e\n        \u003c/td\u003e\n        \u003ctd\u003e\n            Core logic layer that maintains basic controllers,\n            DAOs, translator, common exceptions, routing etc.\n        \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\n            \u003cb\u003e\n            \u003ca href=\"https://github.com/mathematicator-core/tokenizer\"\u003e\n                Tokenizer\n            \u003c/a\u003e\n            \u003c/b\u003e\n        \u003c/td\u003e\n        \u003ctd\u003e\n            Tokenizer that can convert string (user input / LaTeX) to numbers\n            and operators.\n        \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\n            \u003cb\u003e\n            \u003ca href=\"https://github.com/mathematicator-core/numbers\"\u003e\n                Numbers\n            \u003c/a\u003e\n            \u003c/b\u003e\n        \u003c/td\u003e\n        \u003ctd\u003e\n            Fast \u0026 secure storage for numbers with arbitrary precision.\n            It supports Human string and LaTeX output and basic conversions.\n        \u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/table\u003e\n\n**Third-party packages:**\n\n⚠️ Not guaranteed!\n\n\u003ctable\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\n            \u003cb\u003e\n            \u003ca href=\"https://github.com/cothema/math-php-api\"\u003e\n                REST API\n            \u003c/a\u003e\n            \u003c/b\u003e\n        \u003c/td\u003e\n        \u003ctd\u003e\n            Install the whole pack as a REST API service\n            on your server (Docker ready) or\n            access it via public cloud REST API.\n        \u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/table\u003e\n\n\n## Contribution\n\n\u003e Please help to improve this documentation by sending a Pull request.\n\n### Tests\n\nAll new contributions should have its unit tests in `/tests` directory.\n\nBefore you send a PR, please, check all tests pass.\n\nThis package uses [Nette Tester](https://tester.nette.org/).\nYou can run tests via command:\n```bash\ncomposer test\n````\n\nFor benchmarking, we use [phpbench](https://github.com/phpbench/phpbench).\nYou can run benchmarks this way:\n```bash\ncomposer global require phpbench/phpbench @dev # only the first time\nphpbench run\n````\n\nBefore PR, please run complete code check via command:\n```bash\ncomposer cs:install # only the first time\ncomposer fix # otherwise pre-commit hook can fail\n````\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathematicator-core%2Fnumbers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmathematicator-core%2Fnumbers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathematicator-core%2Fnumbers/lists"}