Created
August 21, 2014 09:39
-
-
Save fulmicoton/11b3fc4a3eb81e6eccb4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
template<typename TFloat> | |
bool _Field::to_float(TFloat* dest) const { | |
/* */ | |
if (length == 0) { | |
return false; | |
} | |
int offset=0; | |
int sign = 1; | |
int int_part = 0; | |
TFloat dec_part = 0.0; | |
TFloat res = 0.0; | |
TFloat dec = 1.0; | |
pychar* cur = s; | |
if (*cur == '+') { | |
cur++; | |
offset++; | |
} | |
else if (*cur == '-') { | |
sign = -1; | |
cur++; | |
offset++; | |
} | |
for (; offset < length;) { | |
int_part *= 10; | |
int digit = (*cur) - '0'; | |
if ((digit < 0) || (digit > 9)) { | |
if ((*cur) == '.') { | |
offset++; | |
cur++; | |
break; | |
} | |
else { | |
return false; | |
} | |
} | |
int_part += digit; | |
offset++; | |
cur++; | |
} | |
for (; offset < length;) { | |
int digit = (*cur) - '0'; | |
if ((digit < 0) || (digit > 9)) { | |
return false; | |
} | |
dec /= 10.; | |
dec_part += dec * digit; | |
cur++; | |
offset++; | |
} | |
res = int_part + dec_part; | |
if (sign == 1) { | |
*dest = res; | |
} | |
else { | |
*dest = -res; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment