{"id":15395750,"url":"https://github.com/lifthrasiir/hexf","last_synced_at":"2025-05-16T18:05:48.779Z","repository":{"id":19700215,"uuid":"87681100","full_name":"lifthrasiir/hexf","owner":"lifthrasiir","description":"Hexadecimal float support for Rust","archived":false,"fork":false,"pushed_at":"2024-12-04T06:47:39.000Z","size":36,"stargazers_count":38,"open_issues_count":8,"forks_count":11,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-11T02:57:37.146Z","etag":null,"topics":["floating-point","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"0bsd","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lifthrasiir.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}},"created_at":"2017-04-09T03:50:39.000Z","updated_at":"2024-12-04T06:47:44.000Z","dependencies_parsed_at":"2024-06-19T03:03:55.849Z","dependency_job_id":"be1775f1-a4de-44d3-9d5b-56b41862497f","html_url":"https://github.com/lifthrasiir/hexf","commit_stats":{"total_commits":29,"total_committers":9,"mean_commits":"3.2222222222222223","dds":0.7241379310344828,"last_synced_commit":"1df47db2b34207832cba60d98556d7a547667e79"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lifthrasiir%2Fhexf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lifthrasiir%2Fhexf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lifthrasiir%2Fhexf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lifthrasiir%2Fhexf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lifthrasiir","download_url":"https://codeload.github.com/lifthrasiir/hexf/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248598350,"owners_count":21131081,"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":["floating-point","rust"],"created_at":"2024-10-01T15:29:26.507Z","updated_at":"2025-04-12T16:40:29.715Z","avatar_url":"https://github.com/lifthrasiir.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hexf\n\n[![Hexf on crates.io][cratesio-image]][cratesio]\n[![Hexf on docs.rs][docsrs-image]][docsrs]\n\n[cratesio-image]: https://img.shields.io/crates/v/hexf.svg\n[cratesio]: https://crates.io/crates/hexf\n[docsrs-image]: https://docs.rs/hexf/badge.svg\n[docsrs]: https://docs.rs/hexf/\n\nHexadecimal float support for Rust 1.43 or later. (For earlier versions, try `0.1.0`)\n\n```rust\nuse hexf::hexf64;\n\nassert_eq!(hexf64!(\"0x1.999999999999ap-4\"), 0.1f64);\n```\n\nThe literal is explicitly typed,\nand should match to the pattern `SIGN \"0x\" INTEGRAL \".\" FRACTIONAL \"p\" EXPSIGN EXPDIGITS`, where:\n\n* All Latin letters are matched case-insensitively;\n\n* `SIGN` and `EXPSIGN` are either `+`, `-` or empty;\n\n* `INTEGRAL` and `FRACTIONAL` are one or more hexadecimal digits,\n  optionally separated by or ending with exactly one underscore (`_`) (but cannot begin with it);\n\n* At least one of `INTEGRAL` or `FRACTIONAL` should be present\n  (`1.0` or `.0` or `1.` is allowed, `1` is not);\n\n* `EXPDIGITS` is decimal digits,\n  optionally separated by or beginning or ending with exactly one underscore (`_`).\n\nIt is a compile-time error to put an invalid literal.\n\n```rust,ignore\n// hexf32! failed: invalid hexadecimal float literal\nlet invalid = hexf32!(\"42\");\n```\n\nIt is also a compile-time error to put a literal\nthat would be not exactly representable in the target type.\n\n```rust,ignore\n// hexf32! failed: cannot exactly represent float in target type\nlet inexact = hexf32!(\"0x1.99999bp-4\");\n\n// hexf32! failed: cannot exactly represent float in target type\nlet inexact_subnormal = hexf32!(\"0x1.8p-149\");\n\n// hexf64! failed: cannot exactly represent float in target type\nlet overflow = hexf64!(\"0x1.0p1024\");\n\n// hexf64! failed: cannot exactly represent float in target type\nlet underflow = hexf64!(\"0x1.0p-1075\");\n```\n\nThe crate (and also a standalone `hexf-parse` crate) provides\n`parse_hexf32` and `parse_hexf64` functions,\nwhich allows parsing failures (reported via a `ParseHexfError` type).\nThese functions will allow for interleaved underscores only if the second parameter is true;\nthis is added for the consistency, because Rust allows for underscores in numeric literals,\nbut not in the standard library (`\"3_4\".parse::\u003ci32\u003e()` is an error).\n\n## How does it work?\n\nThis crate heavily relies on the fact that\nthe recent enough Rust compiler can correctly print *and* read a floating point number.\nSo the actual implementation of this crate is, well, done by\nprinting the parsed hexadecimal float back to the correct decimal digits,\nwhich is picked up by the compiler to produce an exact bit pattern.\n\nWait, then what's the point of hexadecimal floats?\nThe answer is that **they are \"invented\" by ISO C99 to avoid implementation pitfalls**.\nIdeally it should be possible to enumerate enough fractional digits\nto get the correctly rounded bit pattern,\nbut many implementations didn't\n(quite understandably, because it is actually [quite hard][dec2flt-paper]).\nSo the Standard has made a compromise:\nin the conforming implementation decimal floats should parse to\nvery close to, but not exactly, the correctly rounded number:\n\n\u003e The significand part is interpreted as a (decimal or hexadecimal) rational number;\n\u003e the digit sequence in the exponent part is interpreted as a decimal integer. [...]\n\u003e For decimal floating constants,\n\u003e and also for hexadecimal floating constants when FLT_RADIX is not a power of 2,\n\u003e the result is **either the nearest representable value,\n\u003e or the larger or smaller representable value\n\u003e immediately adjacent to the nearest representable value**,\n\u003e chosen in an implementation-defined manner. [...]\n\u003e\n\u003e —ISO C99, Section 6.4.4.2 Floating constants, Paragraph 3 (emphases mine)\n\nIndeed, it is relatively easier to parse decimal floats in that accuracy.\nHexadecimal floats are born out of this legacy, but Rust doesn't have to!\nHexadecimal floats can be still useful for manually writing float bits down,\nor for converting from other languages, however.\nThis crate exists for those rarer use cases.\n\nSee [rust-lang/rust#1433][issue-1433] for the more context.\n\n[dec2flt-paper]: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.45.4152\n[issue-1433]: https://github.com/rust-lang/rust/issues/1433#issuecomment-288184018\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flifthrasiir%2Fhexf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flifthrasiir%2Fhexf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flifthrasiir%2Fhexf/lists"}