Fix the precision of StandardComponents::types::Integer

Signed-off-by: Username404 <w.iron.zombie@gmail.com>
This commit is contained in:
Username404 2022-03-22 18:17:36 +01:00
parent ca3b030fed
commit 88d0774d30
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
3 changed files with 12 additions and 6 deletions

View File

@ -106,8 +106,10 @@ namespace StandardComponents {
}; };
namespace types { namespace types {
struct Integer: ParseComponent { struct Integer: ParseComponent {
long double value; typedef uint_fast8_t precision_type;
Integer(const string& value): value(stold(value)) {} const long double value;
const precision_type precision;
Integer(const long double& value, const precision_type& precision): value(value), precision(precision) {}
}; };
struct String: ParseComponent { struct String: ParseComponent {
const string content; const string content;

View File

@ -44,13 +44,16 @@ namespace Parser {
switch (current.toktype) { switch (current.toktype) {
case NUMBER: { case NUMBER: {
types::Integer Int = current.toktext; long double v = stoul(current.toktext);
if (i > 0 && lexed[i - 1].toktype == HYPHEN) v = -v;
types::Integer::precision_type p = 0;
if (nextAre({DOT, NUMBER})) { if (nextAre({DOT, NUMBER})) {
i += 2; i += 2;
const string& right = lexed[i].toktext; const string& right = lexed[i].toktext;
Int.value += stold(right) / powl(10, right.size()); p = min(static_cast<int>(right.size()), numeric_limits<long double>::digits10);
v += copysign(stold(right.substr(0, p)) / powl(10, p), v);
} }
parseTree << Int; parseTree << types::Integer(v, p);
break; break;
} }
case STRING: parseTree << types::String(current.toktext.data()); break; case STRING: parseTree << types::String(current.toktext.data()); break;

View File

@ -9,6 +9,7 @@
#include <cstring> #include <cstring>
#include <unordered_map> #include <unordered_map>
#include <typeindex> #include <typeindex>
#include <limits>
#ifdef __GNUC__ #ifdef __GNUC__
#include <cxxabi.h> #include <cxxabi.h>
#endif #endif
@ -133,7 +134,7 @@ public:
separate_transpileTree(parseComponent, ", "); separate_transpileTree(parseComponent, ", ");
output << ')'; output << ')';
), ),
make_task(StandardComponents::types::Integer, output << parseComponent.value;) make_task(StandardComponents::types::Integer, output << setprecision(parseComponent.precision) << fixed << parseComponent.value;)
})); }));
return staticMap; return staticMap;
}; };