Skip to content

Commit 1b48098

Browse files
arturbacUF4007
andauthored
fixing from string and tests for string convertion (#15)
* tests for string convertion * fixing from string --------- Co-authored-by: Artur Bać <artur@ebasoft.com.pl> Co-authored-by: UF4007 <142472038+UF4007@users.noreply.github.com>
1 parent c783ac5 commit 1b48098

File tree

4 files changed

+343
-119
lines changed

4 files changed

+343
-119
lines changed

fixed_lib/include/fixedmath/fixed_string.h

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,27 @@ namespace func
142142

143143
// 16 bits of precision = 65536 units
144144
// Each position after decimal point is a power of 10:
145-
// .1 = 6553.6 * 65536
146-
// .01 = 655.36 * 65536
147-
// .001 = 65.536 * 65536
145+
// .1 = 6553.6 * 2^32
146+
// .01 = 655.36 * 2^32
147+
// .001 = 65.536 * 2^32
148148
// etc.
149149
fixed_internal fraction{0};
150-
constexpr fixed_internal power[5]{429496730, 42949673, 4294967, 429497, 42950}; // 10^-1 * 2^16 (rounded down)
150+
static constexpr fixed_internal power[14]{
151+
28147497671066,
152+
2814749767107,
153+
281474976711,
154+
28147497671,
155+
2814749767,
156+
281474977,
157+
28147498,
158+
2814750,
159+
281475,
160+
28148,
161+
2815,
162+
281,
163+
28,
164+
3
165+
}; // 10^-1 * 2^16 (rounded down)
151166
uint8_t i{0};
152167

153168
while(pos < str.size() and detail::isdigit(str[pos]))
@@ -165,7 +180,7 @@ namespace func
165180
++i;
166181
}
167182

168-
result += fraction >> 16;
183+
result += fraction >> 32;
169184
}
170185

171186
// After parsing, skip all spaces and digits; if any other char, return nullopt
@@ -199,7 +214,7 @@ namespace func
199214
std::string result;
200215

201216
// Handle negative numbers
202-
bool negative{value.v < 0};
217+
bool const negative{value.v < 0};
203218
fixed_internal abs_value{negative ? -value.v : value.v};
204219

205220
// Extract integer part (high 48 bits)

unit_tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,5 @@ add_fixedmath_ut( misc_functions_ut )
6363
add_fixedmath_ut( sin_cos_ut )
6464
add_fixedmath_ut( tan_ut )
6565
add_fixedmath_ut( atan_ut )
66-
add_fixedmath_ut( fixed_string_unittest )
66+
add_fixedmath_ut( string_convertion_ut )
6767

unit_tests/fixed_string_unittest.cc

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)