{"id":13995597,"url":"https://github.com/Flight-School/FloatingPointApproximation","last_synced_at":"2025-07-22T22:31:18.550Z","repository":{"id":63909973,"uuid":"139590686","full_name":"Flight-School/FloatingPointApproximation","owner":"Flight-School","description":"A correct way to determine if two floating-point numbers are approximately equal to one another in Swift","archived":true,"fork":false,"pushed_at":"2019-05-24T16:03:34.000Z","size":20,"stargazers_count":52,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-07-11T14:48:18.814Z","etag":null,"topics":["approximation","floating-point","number","swift","ulp"],"latest_commit_sha":null,"homepage":"https://flight.school/books/numbers","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/Flight-School.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["mattt"],"custom":"https://flight.school/books/numbers"}},"created_at":"2018-07-03T13:52:01.000Z","updated_at":"2023-01-28T11:57:43.000Z","dependencies_parsed_at":"2022-11-29T07:06:33.310Z","dependency_job_id":null,"html_url":"https://github.com/Flight-School/FloatingPointApproximation","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/Flight-School/FloatingPointApproximation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flight-School%2FFloatingPointApproximation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flight-School%2FFloatingPointApproximation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flight-School%2FFloatingPointApproximation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flight-School%2FFloatingPointApproximation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Flight-School","download_url":"https://codeload.github.com/Flight-School/FloatingPointApproximation/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flight-School%2FFloatingPointApproximation/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266585740,"owners_count":23952163,"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","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["approximation","floating-point","number","swift","ulp"],"created_at":"2024-08-09T14:03:30.046Z","updated_at":"2025-07-22T22:31:18.286Z","avatar_url":"https://github.com/Flight-School.png","language":"Swift","readme":"# FloatingPointApproximation\n\n[![Build Status][build status badge]][build status]\n\nA correct way to determine if two floating-point numbers\nare approximately equal to one another.\n\nThis functionality is discussed in Chapter 3 of\n[Flight School Guide to Swift Numbers](https://flight.school/books/numbers).\n\n## Requirements\n\n- Swift 4.0+\n\n## Installation\n\n### Swift Package Manager\n\nAdd the FloatingPointApproximation package to your target dependencies in `Package.swift`:\n\n```swift\nimport PackageDescription\n\nlet package = Package(\n  name: \"YourProject\",\n  dependencies: [\n    .package(\n        url: \"https://github.com/Flight-School/FloatingPointApproximation\",\n        from: \"1.0.0\"\n    ),\n  ]\n)\n```\n\nThen run the `swift build` command to build your project.\n\n### Carthage\n\nTo use `FloatingPointApproximation` in your Xcode project using Carthage,\nspecify it in `Cartfile`:\n\n```\ngithub \"Flight-School/FloatingPointApproximation\" ~\u003e 1.0.0\n```\n\nThen run the `carthage update` command to build the framework,\nand drag the built FloatingPointApproximation.framework into your Xcode project.\n\n## Usage\n\nFloating-point arithmetic can produce unexpected results,\nsuch as `0.1 + 0.2 != 0.3`.\nThe reason for this is that many fractional numbers,\nincluding `0.1`, `0.2`, and `0.3`,\ncannot be precisely expressed in a binary number representation.\n\nA common mistake is to use an arbitrarily small constant\n(such as `.ulpOfOne`)\nto determine whether two floating-point numbers are approximately equal.\nFor example:\n\n```swift\nlet actual = 0.1 + 0.2\nlet expected = 0.3\nabs(expected - actual) \u003c .ulpOfOne // true\n```\n\nHowever, this doesn't work for large scale numbers:\n\n```swift\nlet actual = 1e25 + 2e25\nlet expected = 3e25\nabs(expected - actual) \u003c .ulpOfOne // false\n```\n\nA better approach for determining approximate equality\nwould be to count how many representable values, or ULPs,\nexist between two floating-point numbers.\n\nThe `==~` operator (and its complement, `!=~`)\ndefined by this package\nreturns a Boolean value indicating whether\ntwo floating-point numbers are approximately equal.\n\n```swift\nimport FloatingPointApproximation\n\n0.1 + 0.2 == 0.3 // false\n0.1 + 0.2 ==~ 0.3 // true\n```\n\nFloating-point numbers are defined to be approximately equal\nif they are within one _unit of least precision_, or\n[ULP](https://en.wikipedia.org/wiki/Unit_in_the_last_place),\nof one another.\n\nBecause of how Swift implements floating-point numbers,\nthe implementation of the `==~` operator is quite simple:\n\n```swift\nfunc ==~\u003cT\u003e (lhs: T, rhs: T) -\u003e Bool where T: FloatingPoint  {\n    return lhs == rhs || lhs.nextDown == rhs || lhs.nextUp == rhs\n}\n```\n\nA more complete approach combines both absolute and relative comparisons.\nThe `isApproximatelyEqual(to:within:maximumULPs:)` method\ndetermines whether a floating-point number\nis approximately equal to another value\nby first checking to see if it is within a given absolute margin, if provided,\nand then checking to see if it falls within a given number of ULPs:\n\n```swift\nimport FloatingPointApproximation\n\n(0.1 + 0.2).isApproximatelyEqual(to: 0.3, within: 1e-12, maximumULPs: 2)\n```\n\nUltimately, it's your responsibility to determine how to compare\ntwo floating-point numbers for approximate equality\nbased on the requirements of your domain.\n\n## License\n\nMIT\n\n## Contact\n\nMattt ([@mattt](https://twitter.com/mattt))\n\n[build status]: https://travis-ci.org/Flight-School/FloatingPointApproximation\n[build status badge]: https://api.travis-ci.com/Flight-School/FloatingPointApproximation.svg?branch=master\n","funding_links":["https://github.com/sponsors/mattt","https://flight.school/books/numbers"],"categories":["Swift"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFlight-School%2FFloatingPointApproximation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FFlight-School%2FFloatingPointApproximation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFlight-School%2FFloatingPointApproximation/lists"}