{"id":19405835,"url":"https://github.com/vladkens/fractions-math","last_synced_at":"2026-05-12T21:37:28.646Z","repository":{"id":171289245,"uuid":"647698009","full_name":"vladkens/fractions-math","owner":"vladkens","description":"🐍🔢 Implementing fractions module from The Python Standard Library on TypeScript.","archived":false,"fork":false,"pushed_at":"2023-06-01T16:36:49.000Z","size":43,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-03-15T15:08:46.612Z","etag":null,"topics":["cjs","esm","fractions","math","npm-modules","printer","rational-numbers","unicode"],"latest_commit_sha":null,"homepage":"https://npm.im/fractions-math","language":"TypeScript","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/vladkens.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/codeowners","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"vladkens","buy_me_a_coffee":"vladkens"}},"created_at":"2023-05-31T10:34:29.000Z","updated_at":"2024-03-11T11:55:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"c39a4e98-ab44-4df5-89bf-0940f7e861d0","html_url":"https://github.com/vladkens/fractions-math","commit_stats":null,"previous_names":["vladkens/fractions-math"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vladkens%2Ffractions-math","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vladkens%2Ffractions-math/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vladkens%2Ffractions-math/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vladkens%2Ffractions-math/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vladkens","download_url":"https://codeload.github.com/vladkens/fractions-math/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240582044,"owners_count":19824145,"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":["cjs","esm","fractions","math","npm-modules","printer","rational-numbers","unicode"],"created_at":"2024-11-10T11:39:43.441Z","updated_at":"2026-05-12T21:37:28.637Z","avatar_url":"https://github.com/vladkens.png","language":"TypeScript","funding_links":["https://github.com/sponsors/vladkens","https://buymeacoffee.com/vladkens"],"categories":[],"sub_categories":[],"readme":"# fractions-math\n\n\u003cdiv align=\"center\"\u003e\n\n[\u003cimg src=\"https://badges.ws/npm/v/fractions-math\" alt=\"version\" /\u003e](https://npmjs.org/package/fractions-math)\n[\u003cimg src=\"https://packagephobia.com/badge?p=fractions-math\" alt=\"size\" /\u003e](https://packagephobia.now.sh/result?p=fractions-math)\n[\u003cimg src=\"https://badges.ws/npm/dm/fractions-math\" alt=\"downloads\" /\u003e](https://npmjs.org/package/fractions-math)\n[\u003cimg src=\"https://badges.ws/github/license/vladkens/fractions-math\" alt=\"license\" /\u003e](https://github.com/vladkens/fractions-math/blob/main/LICENSE)\n[\u003cimg src=\"https://badges.ws/badge/-/buy%20me%20a%20coffee/ff813f?icon=buymeacoffee\u0026label\" alt=\"donate\" /\u003e](https://buymeacoffee.com/vladkens)\n\n\u003c/div\u003e\n\nPython-style fractions for TypeScript, with exact arithmetic, string parsing, and mixed-number output.\n\n- **1.5 KB brotli.** `dist/main.mjs` is 1489 bytes brotli and 1632 bytes gzip.\n- **Exact math.** Add, subtract, multiply, divide, compare, and reduce without floating-point drift.\n- **Flexible input.** Parse `1/2`, `1 1/2`, `0.125`, tuples, numbers, or existing `Fraction` instances.\n- **Readable output.** Print fractions as `3/4`, `1 1/2`, or Unicode forms like `1½`.\n\nUnlike generic math helpers, this stays close to Python's `fractions` module and includes ASCII/Unicode formatting that libraries usually leave out.\n\n## Install\n\n```sh\nnpm i fractions-math\n```\n\n```sh\nyarn add fractions-math\n```\n\n## Usage\n\n```ts\nimport { Fraction, fraq, fraqOrNull, gcd } from \"fractions-math\"\n```\n\n### Create fractions\n\n**`fraq`** converts strings, numbers, tuples, and existing fractions into a `Fraction`.\n\n```ts\nfraq(2).toPair() // -\u003e [2, 1]\nfraq([3, 4]).toPair() // -\u003e [3, 4]\nfraq(\"1 1/2\").toPair() // -\u003e [3, 2]\nfraq(\"0.125\").reduce().toPair() // -\u003e [1, 8]\n```\n\n**`new Fraction`** gives you explicit control over numerator, denominator, and optional reduction.\n\n```ts\nnew Fraction(5, 10).toPair() // -\u003e [5, 10]\nnew Fraction(5, 10, true).toPair() // -\u003e [1, 2]\nnew Fraction(-3, 4).toString() // -\u003e \"-3/4\"\n```\n\n**`fraqOrNull`** returns `null` instead of throwing on invalid input.\n\n```ts\nfraqOrNull(\"1/2\")?.toString() // -\u003e \"1/2\"\nfraqOrNull(\"nope\") // -\u003e null\n```\n\n### Do exact arithmetic\n\n**`add` / `sub` / `mul` / `div`** keep results as fractions instead of falling back to floats.\n\n```ts\nconst price = fraq(\"1 1/2\")\n\nprice.add(\"1/4\").toString() // -\u003e \"7/4\"\nprice.sub(\"1/2\").toString() // -\u003e \"1\"\nprice.mul(3).toString() // -\u003e \"9/2\"\nprice.div(\"3/4\").toString() // -\u003e \"2\"\n```\n\n**`reduce`** normalizes a fraction only when you want it.\n\n```ts\nfraq(\"15/10\").toString() // -\u003e \"15/10\"\nfraq(\"15/10\").reduce().toString() // -\u003e \"3/2\"\n```\n\n**`limit`** finds a nearby fraction with a bounded denominator.\n\n```ts\nfraq(Math.PI).limit(1000).toString() // -\u003e \"355/113\"\nfraq(\"0.3333\").limit(16).toString() // -\u003e \"1/3\"\n```\n\n### Compare values\n\n**`eq` / `lt` / `lte` / `gt` / `gte`** compare rational values directly.\n\n```ts\nfraq(\"1/2\").eq(0.5) // -\u003e true\nfraq(\"1/2\").lt(\"2/3\") // -\u003e true\nfraq(\"3/4\").gte(\"6/8\") // -\u003e true\n```\n\n**`gcd`** is also exported if you need integer greatest common divisor logic.\n\n```ts\ngcd(24, 36) // -\u003e 12\ngcd(999, 1000) // -\u003e 1\n```\n\n### Format output\n\n**`toString` / `toNumber` / `toPair`** convert fractions into the shape you need.\n\n```ts\nconst f = fraq(\"3/2\")\n\nf.toString() // -\u003e \"3/2\"\nf.toNumber() // -\u003e 1.5\nf.toPair() // -\u003e [3, 2]\n```\n\n**`toParts`** splits a value into sign, whole number, numerator, and denominator.\n\n```ts\nfraq(\"7/4\").toParts() // -\u003e { s: 1, c: 1, n: 3, d: 4 }\nfraq(\"-1/2\").toParts() // -\u003e { s: -1, c: 0, n: 1, d: 2 }\n```\n\n**`toAscii` / `toUnicode`** print friendly mixed fractions.\n\n```ts\nfraq(\"3/2\").toAscii() // -\u003e \"1 1/2\"\nfraq(\"3/2\").toUnicode() // -\u003e \"1½\"\nfraq(\"1/64\").toAscii() // -\u003e \"1\"\nfraq(\"1/64\").toAscii(64) // -\u003e \"1/64\"\nfraq(\"1/64\").toUnicode(64) // -\u003e \"¹⁄₆₄\"\n```\n\n## API\n\n```ts\ntype Fraq = Fraction | [number, number] | number | string\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvladkens%2Ffractions-math","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvladkens%2Ffractions-math","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvladkens%2Ffractions-math/lists"}