https://github.com/clemensmanert/fas Skip to content Navigation Menu Toggle navigation Sign in * Product + GitHub Copilot Write better code with AI + Security Find and fix vulnerabilities + Actions Automate any workflow + Codespaces Instant dev environments + Issues Plan and track work + Code Review Manage code changes + Discussions Collaborate outside of code + Code Search Find more, search less Explore + All features + Documentation + GitHub Skills + Blog * Solutions By company size + Enterprises + Small and medium teams + Startups By use case + DevSecOps + DevOps + CI/CD + View all use cases By industry + Healthcare + Financial services + Manufacturing + Government + View all industries View all solutions * Resources Topics + AI + DevOps + Security + Software Development + View all Explore + Learning Pathways + White papers, Ebooks, Webinars + Customer Stories + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Enterprise + Enterprise platform AI-powered developer platform Available add-ons + Advanced Security Enterprise-grade security features + GitHub Copilot Enterprise-grade AI features + Premium Support Enterprise-grade 24/7 support * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up Reseting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} clemensmanert / fas Public * Notifications You must be signed in to change notification settings * Fork 0 * Star 23 A floating point arithmetic which works with types of any mantissa, exponent or base in modern header-only C++. 23 stars 0 forks Branches Tags Activity Star Notifications You must be signed in to change notification settings * Code * Issues 0 * Pull requests 0 * Actions * Projects 0 * Security * Insights Additional navigation options * Code * Issues * Pull requests * Actions * Projects * Security * Insights clemensmanert/fas master BranchesTags Go to file Code Folders and files Name Name Last commit Last commit message date Latest commit History 19 Commits .github/workflows .github/ workflows fas fas tests tests .clang-format .clang-format .gitignore .gitignore CMakeLists.txt CMakeLists.txt README.md README.md View all files Repository files navigation * README Build Status Ubuntu fas - a floating point arithmetic in software fas is floating point arithmetic for arbitrary mantissa and exponent types in modern header-only C++. It lets you construct various different float types using template parameters for the mantissa, exponent and base. The constructed float-types look and fell like a native float/double for arithmetic operations. Furthermore all methods are performed on the stack and do not require any heap space. fas is header-only. Features Choose your desired mantissa and exponent fas::Float f; This will result in a float using a signed 16 bit mantissa and a signed 8 bit exponent. Arithmetical operators The arithmetical operations are supported using their corresponding operators + - * / += -= *= /= ++ -- fas::Float f1 = 10; auto f2 = f1; f2 = -f1; // => -10 f2 = f1 + 10; // => 20 f2 = f1 - f1; // => 0 f2 = f1 * f1; // => 100 f2 = f1 / 20; // => 0.5 f2 += 1; // => 1.5 f2 -= 1; // => 0.5 f2 *= 2; // => 1 f2 /= 2; // => 0.5 f1++; // => 11 ++f1; // => 12 f1--; // => 11 --f1; // => 10 Type boundaries Each type knows its boundaries: * MAX() returns the largest value * MIN() returns the smallest positive value * LOWEST() returns the smallest value The naming is the same as with internal float/double. There are overloadings for std::numeric_limits available: fas::Float ::MAX() returns the same as std::numeric_limits >::MAX() which is approx 2.16079e+40. Type traits fas supports type traits: is_fundamental::value // => true is_floating_point::value // => true is_arithmetic::value // => true is_scalar::value // => true is_object::value // => true Everything is a constant expression constexpr c = fas::Float(1); Even the operations' results: constexpr c1 = fas::Float(1); constexpr c2 = fas::Float(2); constexpr c3 = c1 + c2; // => 3 iostream support fas offers an output stream overload, which can be used for std::cout: #include "fas/stream.hpp> ... fas::Float fas_float(1); fas_float /= 3; std::cout << fas_float << "\n"; // => 70 * 7 ^ fffffffd [?] 0.32653061224489793313 Arbitrary base Typical floats such as float or double are to the base of 2. fas allows to construct floats to any base: fas::Float fas_float(0); double cpp_float = 0; for(auto i=0; i<7; ++i) { fas_float += fas::Float(1)/7; cpp_float += 1.0/7; } std::cout << std::setprecision(20); std::cout << fas_float << "\n"; // => exaclty 1 std::cout << cpp_float << "\n"; // => 0.99999999999999977796 The setting of different bases allows to represent specific fractions exaclty. In this case the base is 7, so any fraction by 7 is represented exactly. Compare to the native double which is always to the base of 2, thus can not represent 1/7 exactly. How to use it Download the float.hpp and include it in your source. Don't forget to add it in your include path. Unit tests To build and run unit tests type: mkdir build; cd build; cmake ..; make && ./tests/tests Requirements * The Stl's std::numeric_limits is required the limits of the specified types for mantissa and exponent. * Catch2 is required to build the unit tests. Todo * (configurable) rounding support * exp, sqrt, pow and other math.h functions * string constructor * string representation * value constructor for all ints and other types * a version without stl About A floating point arithmetic which works with types of any mantissa, exponent or base in modern header-only C++. Topics cpp template-metaprogramming header-only cpp17 floating-point float floatingpoint Resources Readme Activity Stars 23 stars Watchers 3 watching Forks 0 forks Report repository Languages * C++ 97.9% * CMake 2.1% Footer (c) 2024 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact * Manage cookies * Do not share my personal information You can't perform that action at this time.