diff --git a/src/rtext.c b/src/rtext.c index eabb1e584e6a..3ce93f6cd4fa 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -1646,7 +1646,11 @@ int TextToInteger(const char *text) // WARNING: Only '.' character is understood as decimal point float TextToFloat(const char *text) { - float value = 0.0f; + // Up to 9 significant digits are read as an integer and scaled once at the end, + // later digits only move the decimal point + unsigned int digits = 0; + int digitCount = 0; + int exponent = 0; float sign = 1.0f; if (text != NULL) @@ -1658,17 +1662,65 @@ float TextToFloat(const char *text) } int i = 0; - for (; ((text[i] >= '0') && (text[i] <= '9')); i++) value = value*10.0f + (float)(text[i] - '0'); + for (; ((text[i] >= '0') && (text[i] <= '9')); i++) + { + if (digitCount < 9) + { + digits = digits*10 + (unsigned int)(text[i] - '0'); + if (digits > 0) digitCount++; + } + else exponent++; + } - if (text[i++] == '.') + if (text[i] == '.') { - float divisor = 10.0f; + i++; for (; ((text[i] >= '0') && (text[i] <= '9')); i++) { - value += ((float)(text[i] - '0'))/divisor; - divisor = divisor*10.0f; + if (digitCount < 9) + { + digits = digits*10 + (unsigned int)(text[i] - '0'); + if (digits > 0) digitCount++; + exponent--; + } } } + + if (((text[i] == 'e') || (text[i] == 'E')) && (i > 0)) + { + int j = i + 1; + int exponentSign = 1; + int exponentValue = 0; + + if ((text[j] == '+') || (text[j] == '-')) + { + if (text[j] == '-') exponentSign = -1; + j++; + } + + if ((text[j] >= '0') && (text[j] <= '9')) + { + for (; ((text[j] >= '0') && (text[j] <= '9')); j++) if (exponentValue < 1000) exponentValue = exponentValue*10 + (text[j] - '0'); + exponent += exponentSign*exponentValue; + } + } + } + + float value = (float)digits; + + if (value != 0.0f) + { + // Scale in exact steps of 1e10, stopping once the value overflows or underflows + while ((exponent > 10) && (value < 3.4e38f)) { value *= 1e10f; exponent -= 10; } + while ((exponent < -10) && (value > 0.0f)) { value /= 1e10f; exponent += 10; } + + // Powers of ten up to 1e10 are exact in a float + int powerCount = (exponent < 0)? -exponent : exponent; + float power = 1.0f; + if (powerCount <= 10) for (int k = 0; k < powerCount; k++) power *= 10.0f; + + if (exponent > 0) value *= power; + else if (exponent < 0) value /= power; } return value*sign;