{"id":18031734,"url":"https://github.com/dagronf/dsfrational","last_synced_at":"2026-05-01T09:31:23.704Z","repository":{"id":63907295,"uuid":"231850504","full_name":"dagronf/DSFRational","owner":"dagronf","description":"A Swift extension for floating point values to return rational components","archived":false,"fork":false,"pushed_at":"2020-01-05T01:31:24.000Z","size":5,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-10T20:40:10.502Z","etag":null,"topics":["objective-c","rational","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","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/dagronf.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":null,"security":null,"support":null}},"created_at":"2020-01-05T01:07:05.000Z","updated_at":"2023-12-05T06:01:55.000Z","dependencies_parsed_at":"2022-11-28T22:53:09.904Z","dependency_job_id":null,"html_url":"https://github.com/dagronf/DSFRational","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FDSFRational","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FDSFRational/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FDSFRational/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FDSFRational/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dagronf","download_url":"https://codeload.github.com/dagronf/DSFRational/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247256072,"owners_count":20909240,"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":["objective-c","rational","swift"],"created_at":"2024-10-30T10:10:36.120Z","updated_at":"2026-05-01T09:31:23.637Z","avatar_url":"https://github.com/dagronf.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Objective-C/Swift Rational class\n\nAn extension for floating point values to return the rational components for that value\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/v/tag/dagronf/DSFRational\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/macOS-10.9+-red\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/iOS-11.0+-blue\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/tvOS-11.0+-orange\" /\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/Swift-5.0-orange.svg\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/License-MIT-lightgrey\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/pod-compatible-informational\" alt=\"CocoaPods\" /\u003e\n    \u003ca href=\"https://swift.org/package-manager\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/spm-compatible-brightgreen.svg?style=flat\" alt=\"Swift Package Manager\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n```swift\n/// Swift example\n\nlet rational = -9.90.rationalValue\n// result.fraction, -9.90)\n// result.description, \"-9 9/10\")\n// result.whole, -9)\n// result.numerator, 9)\n// result.denominator, 10)\n```\n\n```objective-c\n/// Objective-C example\n\nid\u003cRationalRepresentation\u003e result = [DSFRational valueFor:-11.112];\nassert([[result description] isEqual:@\"-11 14/125\"]);\nassert([result whole] == -11);\nassert([result numerator] == 14);\nassert([result denominator] == 125);\nassert([result fraction] == -11.112);\n```\n\n\n\n## Notes\n\nThe algorithm arises from a comment on [GameDev.net](https://www.gamedev.net/) by user John Bolton.\n\n\u003e Here is an algorithm for converting a value to the closest fraction (given a maximum value for the denominator). It is based on Farey Sequences. It is probably much faster than finding GCDs and the results are much more useful. For example, using the suggestions above, .333333 will be converted to 333333/1000000, which is probably not what you want. Using this algorithm, you will get 1/3.\n\n[Click here to see original post](https://www.gamedev.net/forums/topic/354209-how-do-i-convert-a-decimal-to-a-fraction-in-c/?do=findComment\u0026comment=3321651)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdagronf%2Fdsfrational","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdagronf%2Fdsfrational","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdagronf%2Fdsfrational/lists"}