From 77cf2429eb25c168d4edce0e1f885b25deac1e28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Ba=C4=87?= Date: Tue, 23 Jul 2024 22:31:41 +0200 Subject: [PATCH] fix #3 - add example as source code - fix README.md --- CMakeLists.txt | 10 +------ README.md | 62 +++++++++++++++++++++-------------------- examples/CMakeLists.txt | 4 +++ examples/brief-usage.cc | 36 ++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 39 deletions(-) create mode 100644 examples/CMakeLists.txt create mode 100644 examples/brief-usage.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 1abdcf4..23b27b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,12 +71,4 @@ if( PROJECT_IS_TOP_LEVEL) feature_summary(WHAT ALL) endif() - -#-------- -#[[ -add_executable(fixed_math_test main.cc) -target_compile_definitions( fixed_math_test PRIVATE -DFIXEDMATH_ENABLE_SQRT_ABACUS_ALGO=1) - -target_link_libraries( fixed_math_test - fixed_math - agg_fixed )]] +add_subdirectory(examples) diff --git a/README.md b/README.md index b8eb0f5..d185b7e 100644 --- a/README.md +++ b/README.md @@ -104,36 +104,38 @@ The following compilers are confirmed to compile the project successfully with C #include using fixedmath::fixed_t; - -//fixed and all functionality is constexpr so You can declare constants see features [1] -inline constexpr fixed_t foo_constant{ fixedmath::tan( 15 * fixedmath::phi/180) }; - -constexpr fixed_t my_function( fixed_t value ) - { - using namespace fixedmath; - return foo_constant + sin(value) / (1.41_fix - 2*cos(value) / 4); - } - -// converting to/from fixed_t -// construction from other arithmetic types is explicit -fixed_t val { 3.14 }; -fixed_t val { 3u }; - -//- there is no implicit assignment from other types -float some_float{}; -fixed_t some_fixed{}; -... -some_fixed = fixed_t{some_float}; - -//- converting to other arithmetic types coud be done with static cast and is explicit -double some_double { static_cast(some_fixed) }; - -// for constant values postfix operator _fix may be used -some_fixed = some_float * 2.45_fix; //operation with float is promoted to fixed_t -some_double = 4.15 * some_fixed; //operation with double is promoted to double - -std::cout << some_fixed; - +using fixedmath::operator""_fix; + +// fixed and all functionality is constexpr so You can declare constants see features [1] +inline constexpr fixed_t foo_constant{fixedmath::tan(15 * fixedmath::phi / 180)}; + +constexpr fixed_t my_function(fixed_t value) + { + using namespace fixedmath; + return foo_constant + sin(value) / (1.41_fix - 2 * cos(value) / 4); + } + +int main() + { + // converting to/from fixed_t + // construction from other arithmetic types is explicit + fixed_t val1{3.14}; + fixed_t val2{3u}; + + //- there is no implicit assignment from other types + float some_float{3.14f}; + fixed_t some_fixed; + some_fixed = fixed_t{some_float}; + + //- converting to other arithmetic types coud be done with static cast and is explicit + double some_double(some_fixed); + + // for constant values postfix operator _fix may be used + some_fixed = some_float * 2.45_fix; // operation with float is promoted to fixed_t + some_double = 4.15 * some_fixed; // operation with double is promoted to double + + std::cout << some_double << " " << my_function(some_fixed) << std::endl; + } ``` ## Unit Tests diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..f409262 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,4 @@ +add_executable(brief-usage EXCLUDE_FROM_ALL) +target_sources(brief-usage PRIVATE brief-usage.cc) +target_link_libraries(brief-usage PRIVATE fixed_math ) + diff --git a/examples/brief-usage.cc b/examples/brief-usage.cc new file mode 100644 index 0000000..11a09b5 --- /dev/null +++ b/examples/brief-usage.cc @@ -0,0 +1,36 @@ +#include +#include + +using fixedmath::fixed_t; +using fixedmath::operator""_fix; + +// fixed and all functionality is constexpr so You can declare constants see features [1] +inline constexpr fixed_t foo_constant{fixedmath::tan(15 * fixedmath::phi / 180)}; + +constexpr fixed_t my_function(fixed_t value) + { + using namespace fixedmath; + return foo_constant + sin(value) / (1.41_fix - 2 * cos(value) / 4); + } + +int main() + { + // converting to/from fixed_t + // construction from other arithmetic types is explicit + fixed_t val1{3.14}; + fixed_t val2{3u}; + + //- there is no implicit assignment from other types + float some_float{3.14f}; + fixed_t some_fixed; + some_fixed = fixed_t{some_float}; + + //- converting to other arithmetic types coud be done with static cast and is explicit + double some_double(some_fixed); + + // for constant values postfix operator _fix may be used + some_fixed = some_float * 2.45_fix; // operation with float is promoted to fixed_t + some_double = 4.15 * some_fixed; // operation with double is promoted to double + + std::cout << some_double << " " << my_function(some_fixed) << std::endl; + }