-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add example as source code - fix README.md
- Loading branch information
Artur Bać
committed
Jul 23, 2024
1 parent
55b7f53
commit 77cf242
Showing
4 changed files
with
73 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |