Skip to content

Instantly share code, notes, and snippets.

@fulmicoton
Created August 21, 2014 09:39
Show Gist options
  • Save fulmicoton/11b3fc4a3eb81e6eccb4 to your computer and use it in GitHub Desktop.
Save fulmicoton/11b3fc4a3eb81e6eccb4 to your computer and use it in GitHub Desktop.
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