Skip to content

Commit 41e9d41

Browse files
authored
update doc, add exampels to build (#20)
Co-authored-by: Artur Bać <artur@ebasoft.com.pl>
1 parent c5656bd commit 41e9d41

File tree

4 files changed

+85
-71
lines changed

4 files changed

+85
-71
lines changed

docs/index.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ See [documentation of division](division.md) for more details.
4242
- `auto operator /(supported_type1 lh, supported_type2 rh) noexcept`: Operator overload for division.
4343
- `fixed_t & operator /=(fixed_t &lh, supported_type rh) noexcept`: Division assignment.
4444

45+
### Modulo
46+
47+
- `auto operator %(supported_type1 lh, supported_type2 rh) noexcept`: Operator overload for modulo.
48+
- `fixed_t & operator %=(fixed_t &lh, supported_type rh) noexcept`: modulo assignment.
49+
4550
## Bitwise Operations
4651

4752
See [documentation of bitwise operations](bitwise_operations.md) for more details.
@@ -75,6 +80,12 @@ See [documentation of conversion between fixed and arithmetic types](conversion_
7580
- `fixed_t angle_to_radians(integral_type angle) noexcept`: Converts angle 0 - 360 to radians.
7681
- `fixed_t sqrt(fixed_t value) noexcept`: Returns the square root of `value`.
7782
- `fixed_t hypot(fixed_t lh, fixed_t rh) noexcept`: Returns the hypotenuse of `lh` and `rh`.
83+
- `fixed_t fmod(fixed_t value) noexcept`: Returns the modulo of `value`.
84+
- `from_string(std::string_view str) noexcept` : Returns `std::optional<fixed_t>` defined in `fixedmath/fixed_string.h`
85+
- `to_string(fixed_t value, int precision) noexcept` : Returns `std::string` representation of fixed value defined in `fixedmath/fixed_string.h`
86+
- `std::formatter<fixedmath::fixed_t>` defined in `fixedmath/std_formatter.h`
87+
- `operator<<(std::ostream & outstr, fixed_t src)` defined in `fixedmath/iostream.h`
88+
- `operator>>(std::istream & instr, fixed_t & dst)` defined in `fixedmath/iostream.h`
7889

7990
## FixedMath Applications and Use Cases
8091

examples/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,11 @@ add_executable(brief-usage EXCLUDE_FROM_ALL)
22
target_sources(brief-usage PRIVATE brief-usage.cc)
33
target_link_libraries(brief-usage PRIVATE fixed_math )
44

5+
add_executable(iostream EXCLUDE_FROM_ALL)
6+
target_sources(iostream PRIVATE iostream.cc)
7+
target_link_libraries(iostream PRIVATE fixed_math )
8+
9+
add_executable(string_conversion EXCLUDE_FROM_ALL)
10+
target_sources(string_conversion PRIVATE string_conversion.cc)
11+
target_link_libraries(string_conversion PRIVATE fixed_math )
12+

examples/iostream_example.cpp renamed to examples/iostream.cc

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55
#include <iomanip>
66
#include <vector>
77

8-
int main()
9-
{
8+
int main()
9+
{
1010
using namespace fixedmath;
11-
11+
1212
std::cout << "Fixed Math I/O Stream Example" << std::endl;
1313
std::cout << "==============================" << std::endl;
14-
14+
1515
// Output stream examples
1616
std::cout << "\n1. Output Stream Examples:" << std::endl;
17-
17+
1818
// Create some fixed_t values
1919
fixed_t pi = fixed_t{3.14159265};
2020
fixed_t negative = fixed_t{-42.5};
2121
fixed_t large = fixed_t{123456.789};
2222
fixed_t small = fixed_t{0.000125};
2323
fixed_t zero = fixed_t{0};
2424
fixed_t nan = quiet_NaN_result();
25-
25+
2626
// Output with different precision settings
2727
std::cout << "Default precision:" << std::endl;
2828
std::cout << " Pi: " << pi << std::endl;
@@ -31,68 +31,62 @@ int main()
3131
std::cout << " Small: " << small << std::endl;
3232
std::cout << " Zero: " << zero << std::endl;
3333
std::cout << " NaN: " << nan << std::endl;
34-
34+
3535
// Set different precision
3636
std::cout << "\nPrecision 2:" << std::endl;
3737
std::cout << std::setprecision(2);
3838
std::cout << " Pi: " << pi << std::endl;
3939
std::cout << " Negative: " << negative << std::endl;
4040
std::cout << " Large: " << large << std::endl;
41-
41+
4242
std::cout << "\nPrecision 10:" << std::endl;
4343
std::cout << std::setprecision(10);
4444
std::cout << " Pi: " << pi << std::endl;
4545
std::cout << " Small: " << small << std::endl;
46-
46+
4747
// Input stream examples
4848
std::cout << "\n2. Input Stream Examples:" << std::endl;
49-
49+
5050
// Use string streams for testing, simulating user input
51-
const std::vector<std::string> test_inputs = {
52-
"3.14159",
53-
"-42.5",
54-
"0",
55-
"123.456",
56-
"NaN",
57-
"nan",
58-
"invalid"
59-
};
60-
61-
for (const auto& input : test_inputs) {
51+
std::vector<std::string> const test_inputs = {"3.14159", "-42.5", "0", "123.456", "NaN", "nan", "invalid"};
52+
53+
for(auto const & input: test_inputs)
54+
{
6255
std::istringstream iss(input);
6356
fixed_t value;
64-
57+
6558
std::cout << "Parsing \"" << input << "\": ";
66-
if (iss >> value) {
59+
if(iss >> value)
6760
std::cout << "Success, value: " << value << std::endl;
68-
} else {
61+
else
6962
std::cout << "Failed to parse" << std::endl;
7063
}
71-
}
72-
64+
7365
// User input example
7466
std::cout << "\n3. User Input Example:" << std::endl;
7567
std::cout << "Enter a fixed point number (or NaN): ";
76-
68+
7769
fixed_t user_input;
78-
if (std::cin >> user_input) {
70+
if(std::cin >> user_input)
71+
{
7972
std::cout << "You entered: " << user_input << std::endl;
80-
73+
8174
// Demonstrate some operations
8275
std::cout << "Double: " << (user_input * 2) << std::endl;
8376
std::cout << "Square: " << (user_input * user_input) << std::endl;
84-
77+
8578
// Check if it's NaN
86-
if (fobj::isnan(user_input)) {
79+
if(fobj::isnan(user_input))
8780
std::cout << "The value is NaN" << std::endl;
8881
}
89-
} else {
82+
else
83+
{
9084
std::cout << "Invalid input" << std::endl;
9185
// Clear error state
9286
std::cin.clear();
9387
// Ignore remainder of the line
9488
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
95-
}
96-
89+
}
90+
9791
return 0;
98-
}
92+
}

examples/string_conversion_example.cpp renamed to examples/string_conversion.cc

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,82 +4,83 @@
44
#include <string>
55
#include <iomanip>
66

7-
int main()
8-
{
7+
int main()
8+
{
99
using namespace fixedmath;
10-
10+
1111
std::cout << "Fixed Math String Conversion Example (Pure Integer Implementation)" << std::endl;
1212
std::cout << "=============================================================" << std::endl;
13-
13+
1414
// Convert string to fixed_t
1515
std::string input = "3.99994";
1616
auto fixed_pi = from_string(input);
17-
18-
if (fixed_pi) {
17+
18+
if(fixed_pi)
19+
{
1920
std::cout << "Converted string \"" << input << "\" to fixed_t: " << *fixed_pi << std::endl;
20-
21+
2122
// Perform some operations
2223
auto doubled = *fixed_pi * 2;
2324
std::cout << "Doubled value: " << doubled << std::endl;
24-
25+
2526
// Convert back to string with different precisions
2627
std::cout << "String representations with different precisions:" << std::endl;
2728
std::cout << " 2 digits: " << to_string(*fixed_pi, 2) << std::endl;
2829
std::cout << " 4 digits: " << to_string(*fixed_pi, 4) << std::endl;
2930
std::cout << " 6 digits: " << to_string(*fixed_pi, 6) << std::endl;
30-
} else {
31+
}
32+
else
33+
{
3134
std::cout << "Failed to convert \"" << input << "\" to fixed_t" << std::endl;
32-
}
33-
35+
}
36+
3437
std::cout << "\nExact Fraction Representation:" << std::endl;
35-
38+
3639
// Test exact binary fractions
3740
fixed_t binary_fractions[] = {
38-
fixed_t{0.5}, // 1/2
39-
fixed_t{0.25}, // 1/4
40-
fixed_t{0.125}, // 1/8
41-
fixed_t{0.0625}, // 1/16
41+
fixed_t{0.5}, // 1/2
42+
fixed_t{0.25}, // 1/4
43+
fixed_t{0.125}, // 1/8
44+
fixed_t{0.0625}, // 1/16
4245
fixed_t{0.0000152587890625}, // 1/65536
4346
};
44-
45-
for (const auto& value : binary_fractions) {
46-
std::cout << " Value: " << std::setw(10) << value
47-
<< " | String: " << std::setw(10) << to_string(value, 20) << std::endl;
48-
}
49-
47+
48+
for(auto const & value: binary_fractions)
49+
std::cout << " Value: " << std::setw(10) << value << " | String: " << std::setw(10) << to_string(value, 20)
50+
<< std::endl;
51+
5052
std::cout << "\nDisadvantaged Decimal Fractions:" << std::endl;
51-
53+
5254
// Test decimal fractions that don't have exact binary representations
5355
fixed_t decimal_fractions[] = {
5456
fixed_t{0.1}, // 1/10
5557
fixed_t{0.01}, // 1/100
5658
fixed_t{0.001}, // 1/1000
5759
fixed_t{0.33333}, // ~1/3
5860
};
59-
60-
for (const auto& value : decimal_fractions) {
61-
std::cout << " Value: " << std::setw(10) << value
62-
<< " | String: " << std::setw(10) << to_string(value, 6) << std::endl;
63-
}
64-
61+
62+
for(auto const & value: decimal_fractions)
63+
std::cout << " Value: " << std::setw(10) << value << " | String: " << std::setw(10) << to_string(value, 6)
64+
<< std::endl;
65+
6566
std::cout << "\nEdge Cases:" << std::endl;
66-
67+
6768
// Test some invalid inputs
6869
auto invalid = from_string("not a number");
6970
std::cout << " Conversion of \"not a number\": " << (invalid ? "succeeded" : "failed") << std::endl;
70-
71+
7172
// Test various values
7273
fixed_t large_number{12345.6789};
7374
std::cout << " Large number: " << to_string(large_number) << std::endl;
74-
75+
7576
fixed_t negative{-42.5};
7677
std::cout << " Negative number: " << to_string(negative) << std::endl;
77-
78+
7879
fixed_t zero{0};
7980
std::cout << " Zero: " << to_string(zero) << std::endl;
80-
81+
8182
auto nan_value = quiet_NaN_result();
8283
std::cout << " NaN value: " << to_string(nan_value) << std::endl;
83-
84+
8485
return 0;
85-
}
86+
}

0 commit comments

Comments
 (0)