{"id":26278880,"url":"https://github.com/koishi-satori/eirinfixed","last_synced_at":"2025-09-09T16:32:41.697Z","repository":{"id":269381279,"uuid":"907207242","full_name":"Koishi-Satori/EirinFixed","owner":"Koishi-Satori","description":"A C++ fixed point number library.","archived":false,"fork":false,"pushed_at":"2025-04-27T06:41:25.000Z","size":110,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-27T07:29:11.713Z","etag":null,"topics":["cplusplus-20","fixed-point","library","math","mathematics"],"latest_commit_sha":null,"homepage":"https://eirinfixed.readthedocs.io/en/latest/","language":"C++","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/Koishi-Satori.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":"2024-12-23T04:33:25.000Z","updated_at":"2025-04-27T06:41:28.000Z","dependencies_parsed_at":"2025-03-28T11:23:04.649Z","dependency_job_id":"a01b322e-9c8d-49e2-bcc6-3f30ea1938d3","html_url":"https://github.com/Koishi-Satori/EirinFixed","commit_stats":null,"previous_names":["koishi-satori/fixed32","koishi-satori/eirinfixed"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Koishi-Satori%2FEirinFixed","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Koishi-Satori%2FEirinFixed/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Koishi-Satori%2FEirinFixed/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Koishi-Satori%2FEirinFixed/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Koishi-Satori","download_url":"https://codeload.github.com/Koishi-Satori/EirinFixed/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252793561,"owners_count":21805053,"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":["cplusplus-20","fixed-point","library","math","mathematics"],"created_at":"2025-03-14T13:18:22.989Z","updated_at":"2025-05-07T00:43:36.507Z","avatar_url":"https://github.com/Koishi-Satori.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EirinFixed\n\n- Other language: [中文](README.zh-CN.md)\n\nA flexible and high-performance C++ fixed point number library, provides fixed point template class, high precision mathematical operations and basic input and output functions. You can run the benchmarks ```fixed.benchmark``` and ```double.benchmark``` to test for performance differences between the fixed types and the C++ double. Also the benchmark results have been provided in the benchmark directory, running in AMD laptop CPU R7-7735H, with [-O3](benchmark/fixed_benchmark_O3.txt) and [-O2](benchmark/fixed_benchmark_O2.txt) optimization.\nThe output functions support the formatter provided by [Papilio Charontis](https://github.com/HenryAWE/PapilioCharontis).\n\nIt also provides a pre-defined 32bit-width fixed point, with 16bit precision(```fixed32```), and 64bit-width fixed point, with 32bit precision(```fixed64```).\nThe fixed points require same calculation result in different platforms, devices, operator systems and compilers, and this library fulfills this requirement.\nNotice that the fixed64 uses some int128 compiler extension as its IntermediateType, and some compiler might not support it. The ```__msvc_int128.hpp``` in MSVC provides ```std::_Signed128``` and ```std::_Unsigned128```, Clang and GCC in Linux provide ```__int128```.\n\n\n### Create Fixed Point\n\nYou can create a fixed point number with integral or floating types using constructor and literals.\n\n- The literals now only provides for fixed32 and fixed64.\n- The way of constructing from floating type is not that recommended.\n    - Reason: Floating point error in different platforms, devices, operator systems and compilers.\n    - The fixed point needs to make sure same result in different situations.\n```c++\n#include \u003ceirin/fixed.hpp\u003e\n\nusing namespace eirin;\n\nint main(int argc, char** argv)\n{\n    auto fp32_1 = fixed32(495);\n    auto fp32_2 = fixed32(114.514); // using \"114.5\"_f32 is better.\n    auto fp1 = fixed_num\u003cint32_t, int64_t, 20, false\u003e(5);\n    auto fp2 = fixed_num\u003cint32_t, int64_t, 20, false\u003e(5.14);\n    fp32_1 = 114_f32;\n    fp32_2 = 5.14_f32;\n    fp32_1 = \"49.5\"_f32;\n    fp32_2 = \"114.5\"_f32; // multi-platforms work-well!\n    fp32_2 = -\"114.5\"_f32;\n    fp32_2 = \"-114.5\"_f32; // both this and the one above are well.\n    return 0;\n}\n```\n\nYou can also create a fixed point from std::basic_istream or strings.\n\n- ```f32_from_cstring``` and ```fixed_from_cstring``` will return true on success.\n- the ```parse``` functions needs to pass the end of the string.\n    - it will stops when meeting the **first char** of the end string.\n    - for example, 114a.514a will only parse \"114\" part.\n\n```c++\n#include \u003ceirin/fixed.hpp\u003e\n\nusing namespace eirin;\n\nint main(int argc, char** argv)\n{\n    fp1 = 0_f32;\n    fp2 = fixed_num\u003cint32_t, int64_t, 20, false\u003e(0);\n    f32_from_cstring(\"-114.514\", 8, fp1);\n    fixed_from_cstring(\"-5\", 2, fp2);\n    std::cin \u003e\u003e fp;\n    parse(\"114a.514a\", \"a\", fp);\n    return 0;\n}\n```\n\n### Fixed Point Output\n\nYou can use std::ostream or format functions provided by [Papilio Charontis](https://github.com/HenryAWE/PapilioCharontis).\n\n- When using Papilio Charontis, please **make sure** you have include the ```fixed_formatter.hpp``` header file.\n\n\n```c++\n#include \u003ceirin/fixed.hpp\u003e\n#include \u003ceirin/ext/papilio_integration.hpp.hpp\u003e\n\nusing namespace eirin;\n\nint main(int argc, char** argv)\n{\n    std::cout \u003c\u003c \"114.5625\"_f32 \u003c\u003c std::endl;\n    std::cout \u003c\u003c \"-114.5625\"_f32 \u003c\u003c std::endl;\n    papilio::println(\"{:s}\", 114.5625_f32);\n    papilio::println(\"{:i}\", 114.5625_f32);\n    papilio::println(\"{:f}\", 114.5625_f32);\n    return 0;\n}\n```\n\n### From Internal Value\n\n- **NOTE: this is not recommended, UNLESS you know what you're doing.**\n\n```c++\n#include \u003ceirin/fixed.hpp\u003e\n\nusing namespace eirin;\n\nint main(int argc, char** argv)\n{\n    auto fp = fixed32::from_internal_value(7504789);\n    auto log2_10 = fixed32::template from_fixed_num_value\u003c60\u003e(0x35269E12F346E200ll);\n    return 0;\n}\n```\n\n### Fixed Operator Functions\n\nYou can convert fixed point to integral or floating types.\n\n```c++\n#include \u003ceirin/fixed.hpp\u003e\n\nusing namespace eirin;\n\n(int) \"114.5\"_f32;\n(float) \"114.5\"_f32;\n```\n\nYou can perform quadratic operations and module between fixed and fixed, fixed and integral, or integral and fixed.\nThe output will always be fixed point.\n\n```c++\n#include \u003ceirin/fixed.hpp\u003e\n\nusing namespace eirin;\n\nfp1 = \"114.5\"_f32;\nfp2 = \"514\"_f32;\nfp1 + fp2;\nfp1 + 100;\n100 + fp2;\nfp1 *= 2;\nfp1 /= fp2;\n```\n\nThe compare functions are just like normal compare.\n\n### Fixed Mathematical Functions\n\nProvides high precision and fast pure c++ implemention.\n\nThe pre-defined math constants are defined in the fixed point number class.\n\nReferences:\n- Trigonometric Functions: [Efficient Approximations for the Arctangent Function](https://ieeexplore.ieee.org/document/1628884)\n- Square Root: [Fixed point sqrt](https://groups.google.com/g/comp.lang.c/c/IpwKbw0MAxw)\n- Binary Logarithm: [A Fast Binary Logarithm Algorithm](http://www.claysturner.com/dsp/BinaryLogarithm.pdf)\n\nSupported functions:\n- rounding functions\n    - ceil/floor\n    - trunc\n    - round\n- abs\n- min/max\n- sqrt\n- trigonometric functions\n    - sin/cos/tan\n    - asin/acos/atan\n- cbrt\n- log2/log/log10\n- pow/exp\n\n### Supported Compilers\n\nRequires at least C++20.\n\n- xmake \u003e= v2.2.2\n- Any C++ compiler that supports C++20\n\n## License\n[MIT](LICENSE) License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoishi-satori%2Feirinfixed","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkoishi-satori%2Feirinfixed","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoishi-satori%2Feirinfixed/lists"}