{"id":15049516,"url":"https://github.com/clord/fraction","last_synced_at":"2025-04-10T02:12:58.306Z","repository":{"id":56845260,"uuid":"1077992","full_name":"clord/fraction","owner":"clord","description":"provides approximate fractions in ruby","archived":false,"fork":false,"pushed_at":"2019-12-03T21:17:43.000Z","size":10,"stargazers_count":20,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T02:12:45.219Z","etag":null,"topics":["c","fraction","ruby"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/fraction","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/clord.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-11-13T19:21:56.000Z","updated_at":"2022-01-16T16:31:02.000Z","dependencies_parsed_at":"2022-09-08T10:50:46.616Z","dependency_job_id":null,"html_url":"https://github.com/clord/fraction","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clord%2Ffraction","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clord%2Ffraction/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clord%2Ffraction/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clord%2Ffraction/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/clord","download_url":"https://codeload.github.com/clord/fraction/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248142903,"owners_count":21054671,"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":["c","fraction","ruby"],"created_at":"2024-09-24T21:21:05.359Z","updated_at":"2025-04-10T02:12:58.285Z","avatar_url":"https://github.com/clord.png","language":"C","readme":"For a [cooking website][3] I recently worked on,\nI needed to display decimal floating point numbers as fractions. I needed to\ncome up with the fraction closest to what the user typed. For example, 0.33\nshould resolve to ⅓. When I googled for a solution, most of the code I found\nwas slow, buggy, and worst of all, too precise (returning 33/100 for the above example.)\n\n\u003ca class=\"bmc-button\" target=\"_blank\" href=\"https://www.buymeacoffee.com/clord\"\u003e\u003cimg src=\"https://cdn.buymeacoffee.com/buttons/bmc-new-btn-logo.svg\" alt=\"Buy me a coffee\"\u003e\u003cspan style=\"margin-left:15px;font-size:19px !important;\"\u003eBuy me a coffee\u003c/span\u003e\u003c/a\u003e\n\nI decided to widen my search to C, and [found][1] a piece of code on Stack Overflow\nwritten by David Eppstein in 1993.\nIt uses the [theory of continued fractions][2] to approach the correct value,\nbut stops when the denominator reaches some value. The limitation of such an\nalgorithm is that we can't choose to leave out unnatural denominators\n\nSo became `fraction`: it's Eppstein's code in a Ruby gem.\n\nInstall it with `gem`:\n\n    gem install fraction\n\nUsing this gem is easy:\n\n    require 'fraction'\n    num, den = 0.33.to_fraction   # num==1, den==3\n    num, den = 0.33.fraction # legacy name also works\n\nYou can get the error:\n\n    num,den,err = 0.33.to_fraction   #=\u003e [1, 3, -0.0033333333333333]\n\nyou can choose a different maximum denominator than the default value of 10:\n\n    num, den = 0.51.to_fraction(100) #=\u003e [51, 100, 0.0]\n\nThere is also whole fraction support, which factors out any whole numbers:\n\n    whole, num, den, error = 3.5.to_whole_fraction #=\u003e [3, 1, 2, 0]\n\nThe best part of this gem over others is the speed:\n\n    % ruby test.rb\n    I'm Feeling Lucky: 19.145s\n    'fraction' gem: 2.090s\n\nsubtracting the time required for an empty ruby loop, we can conclude the\nalgorithm itself requires only ½ of a second for 1,000,000 iterations on my Mac Pro.\n\n\n[1]: http://stackoverflow.com/questions/95727/how-to-convert-floats-to-human-readable-fractions\n[2]: http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/cfINTRO.html#termdecs\n[3]: http://freshslowcooking.com\n","funding_links":["https://www.buymeacoffee.com/clord"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclord%2Ffraction","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fclord%2Ffraction","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclord%2Ffraction/lists"}