pistring::toFloat/Double/LDouble precise fixes
This commit is contained in:
@@ -79,21 +79,18 @@ const float PIString::ElideCenter = .5f;
|
||||
const float PIString::ElideRight = 1.f;
|
||||
|
||||
|
||||
inline float pow10(const int & e) {return powf(10.f, e);}
|
||||
inline double pow10(const double & e) {return pow(10., e);}
|
||||
inline ldouble pow10(const ldouble & e) {return powl(10.L, e);}
|
||||
|
||||
template <typename T>
|
||||
T toDecimal(const PIString & s) {
|
||||
int part = 0, exp = 0;
|
||||
bool negative = false, negative_exp = false, err = false;
|
||||
T ret = 0., frac = 0., frac_delim = 10.;
|
||||
bool negative = false, negative_exp = false, err = false, has_digit = false;
|
||||
T ret = 0., frac = 0., frac_delim = 1.;
|
||||
for (const PIChar pc: s) {
|
||||
char c = pc.toAscii();
|
||||
switch (part) {
|
||||
case 0: // sign
|
||||
if (pc.isSpace()) continue;
|
||||
if (c >= '0' && c <= '9') {
|
||||
has_digit = true;
|
||||
ret = c - '0';
|
||||
part = 1;
|
||||
continue;
|
||||
@@ -115,6 +112,7 @@ T toDecimal(const PIString & s) {
|
||||
break;
|
||||
case 1: // integer
|
||||
if (c >= '0' && c <= '9') {
|
||||
has_digit = true;
|
||||
ret = ret * 10 + (c - '0');
|
||||
continue;
|
||||
}
|
||||
@@ -122,7 +120,7 @@ T toDecimal(const PIString & s) {
|
||||
part = 2;
|
||||
continue;
|
||||
}
|
||||
if (c == 'e' || c == 'E') {
|
||||
if ((c == 'e' || c == 'E') && has_digit) {
|
||||
part = 3;
|
||||
continue;
|
||||
}
|
||||
@@ -130,11 +128,12 @@ T toDecimal(const PIString & s) {
|
||||
break;
|
||||
case 2: // fractional
|
||||
if (c >= '0' && c <= '9') {
|
||||
frac += (c - '0') / frac_delim;
|
||||
frac_delim *= 10;
|
||||
has_digit = true;
|
||||
frac = frac * 10 + (c - '0');
|
||||
frac_delim *= 10.;
|
||||
continue;
|
||||
}
|
||||
if (c == 'e' || c == 'E') {
|
||||
if ((c == 'e' || c == 'E') && has_digit) {
|
||||
part = 3;
|
||||
continue;
|
||||
}
|
||||
@@ -160,8 +159,9 @@ T toDecimal(const PIString & s) {
|
||||
}
|
||||
if (err) break;
|
||||
}
|
||||
frac /= frac_delim;
|
||||
ret += frac;
|
||||
if (negative) ret = -ret;
|
||||
if (negative && has_digit) ret = -ret;
|
||||
if (exp > 0) {
|
||||
if (negative_exp) ret /= pow10((T)exp);
|
||||
else ret *= pow10((T)exp);
|
||||
|
||||
Reference in New Issue
Block a user