{"id":20810048,"url":"https://github.com/schwa/approximateequality","last_synced_at":"2025-08-24T06:19:09.568Z","repository":{"id":191729334,"uuid":"685275796","full_name":"schwa/ApproximateEquality","owner":"schwa","description":"Swift ApproximateEquality protocol \u0026 macros","archived":false,"fork":false,"pushed_at":"2025-01-07T23:07:19.000Z","size":55,"stargazers_count":8,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-08-19T02:45:31.706Z","etag":null,"topics":["macro","numerics","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/schwa.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-08-30T22:04:37.000Z","updated_at":"2025-01-07T23:07:22.000Z","dependencies_parsed_at":"2024-07-20T00:26:59.392Z","dependency_job_id":"c1e8c66c-5dec-41bf-b0e3-e03220f0a1f4","html_url":"https://github.com/schwa/ApproximateEquality","commit_stats":null,"previous_names":["schwa/approximateequality"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/schwa/ApproximateEquality","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schwa%2FApproximateEquality","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schwa%2FApproximateEquality/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schwa%2FApproximateEquality/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schwa%2FApproximateEquality/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/schwa","download_url":"https://codeload.github.com/schwa/ApproximateEquality/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schwa%2FApproximateEquality/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271805827,"owners_count":24824912,"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-08-24T02:00:11.135Z","response_time":111,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","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":["macro","numerics","swift"],"created_at":"2024-11-17T20:19:08.801Z","updated_at":"2025-08-24T06:19:09.512Z","avatar_url":"https://github.com/schwa.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ApproximateEquality\n\n## ``ApproximateEquality`` protocol\n\nFor numeric types this uses ``isApproximatelyEqual`` from [Swift Numerics](https://www.swift.org/blog/numerics/).\n\n## ``DeriveApproximateEquality`` macro.\n\nAnnotate your types with ``DeriveApproximateEquality`` to automatically conform to ``ApproximateEquality`` and derive an implementation.\n\n### ``DeriveApproximateEquality`` Example\n\n```swift\nimport ApproximateEquality\nimport ApproximateEqualityMacros\nimport CoreGraphics\n\n@DeriveApproximateEquality\npublic struct MyType {\n    var x: Double\n    var y: CGPoint\n    @ApproximateEqualityIgnored\n    var z: Double\n    var w: Float\n}\n\nlet thing1 = MyType(x: 10, y: CGPoint(x: 20, y: 0), z: 30, w: 0)\nlet thing2 = MyType(x: 10, y: CGPoint(x: 20, y: 0), z: 40, w: 0)\nlet error = 0.000_001\nlet thing3 = MyType(x: 10 + error, y: CGPoint(x: 20, y: 0), z: 40, w: 0)\n\nprint(thing1.isApproximatelyEqual(to: thing2, absoluteTolerance: 0.0001))      // true\nprint(thing1.isApproximatelyEqual(to: thing3, absoluteTolerance: 0.0001))      // true\nprint(thing1.isApproximatelyEqual(to: thing3, absoluteTolerance: 0.000_000_1)) // false\n```\n\nExpanding the ``@DeriveApproximateEquality`` macro generates an extension:\n\n```swift\nextension MyType: ApproximateEquality {\n    public func isApproximatelyEqual(to other: Self, absoluteTolerance: Double.Magnitude) -\u003e Bool {\n        x.isApproximatelyEqual(to: other.x, absoluteTolerance: Double.Magnitude(absoluteTolerance))\n        \u0026\u0026 y.isApproximatelyEqual(to: other.y, absoluteTolerance: CGPoint.Magnitude(absoluteTolerance))\n        \u0026\u0026 w.isApproximatelyEqual(to: other.w, absoluteTolerance: Float.Magnitude(absoluteTolerance))\n    }\n}\n```\n\nNote that the property prefixed with ``@ApproximateEqualityIgnored`` is used in the implementation.\n\n## License\n\nMIT.\n\nPortions of thise code are from the Apple-Numerics (\u003chttps://github.com/apple/swift-numerics\u003e) project and are licensed under the Apache License\n\n## TODO\n\n- [ ]: Test against more complex types.\n- [X]: Pass through absolute tolerance, norm etc.\n- [X]: Handle the casting to `Double` in ``DeriveApproximateEquality``\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fschwa%2Fapproximateequality","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fschwa%2Fapproximateequality","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fschwa%2Fapproximateequality/lists"}