Skip to content

Commit

Permalink
fix #3
Browse files Browse the repository at this point in the history
- add example as source code
- fix README.md
  • Loading branch information
Artur Bać committed Jul 23, 2024
1 parent 55b7f53 commit 77cf242
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 39 deletions.
10 changes: 1 addition & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
62 changes: 32 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,36 +104,38 @@ The following compilers are confirmed to compile the project successfully with C
#include <fixedmath/iostream.h>

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<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_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
Expand Down
4 changes: 4 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 )

36 changes: 36 additions & 0 deletions examples/brief-usage.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <fixedmath/fixed_math.hpp>
#include <fixedmath/iostream.h>

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;
}

0 comments on commit 77cf242

Please sign in to comment.