From 2b1e99f3c2157ae9a1b4f2698270676bf7211523 Mon Sep 17 00:00:00 2001 From: jayhemnani9910 <193022578+jayhemnani9910@users.noreply.github.com> Date: Wed, 21 Oct 2026 02:15:36 +0530 Subject: [PATCH 1/3] [rtext] Fix `TextToFloat()` rounding and add exponent support --- src/rtext.c | 57 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/src/rtext.c b/src/rtext.c index eabb1e584e6a..94b69d58e4f5 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -1646,32 +1646,71 @@ int TextToInteger(const char *text) // WARNING: Only '.' character is understood as decimal point float TextToFloat(const char *text) { - float value = 0.0f; - float sign = 1.0f; + // Digits are collected as an integer and scaled once at the end, + // so the value is only rounded when converting to float + double value = 0.0; + double sign = 1.0; + int exponent = 0; if (text != NULL) { if ((text[0] == '+') || (text[0] == '-')) { - if (text[0] == '-') sign = -1.0f; + if (text[0] == '-') sign = -1.0; text++; } + // Digits past what a double holds exactly only move the decimal point 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 (value < 1e18) value = value*10.0 + (double)(text[i] - '0'); + 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 (value < 1e18) + { + value = value*10.0 + (double)(text[i] - '0'); + 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; } } } - return value*sign; + if (value != 0.0) + { + double scale = 1.0; + for (int e = (exponent < 0)? -exponent : exponent; (e > 0) && (scale < 1e300); e--) scale *= 10.0; + + if (exponent < 0) value /= scale; + else value *= scale; + } + + return (float)(value*sign); } // Copy one string to another, returns bytes copied From 2960813cb9840bb949a81e0bcd717c15c6458d9e Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Wed, 21 Oct 2026 14:03:41 +0530 Subject: [PATCH 2/3] [rtext] Use only floats in `TextToFloat()` --- src/rtext.c | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/rtext.c b/src/rtext.c index 94b69d58e4f5..48a22a9f2dc8 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -1646,25 +1646,32 @@ int TextToInteger(const char *text) // WARNING: Only '.' character is understood as decimal point float TextToFloat(const char *text) { - // Digits are collected as an integer and scaled once at the end, - // so the value is only rounded when converting to float - double value = 0.0; - double sign = 1.0; + // Powers of ten a float holds exactly + static const float powersOfTen[11] = { 1e0f, 1e1f, 1e2f, 1e3f, 1e4f, 1e5f, 1e6f, 1e7f, 1e8f, 1e9f, 1e10f }; + + // 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) { if ((text[0] == '+') || (text[0] == '-')) { - if (text[0] == '-') sign = -1.0; + if (text[0] == '-') sign = -1.0f; text++; } - // Digits past what a double holds exactly only move the decimal point int i = 0; for (; ((text[i] >= '0') && (text[i] <= '9')); i++) { - if (value < 1e18) value = value*10.0 + (double)(text[i] - '0'); + if (digitCount < 9) + { + digits = digits*10 + (unsigned int)(text[i] - '0'); + if (digits > 0) digitCount++; + } else exponent++; } @@ -1673,9 +1680,10 @@ float TextToFloat(const char *text) i++; for (; ((text[i] >= '0') && (text[i] <= '9')); i++) { - if (value < 1e18) + if (digitCount < 9) { - value = value*10.0 + (double)(text[i] - '0'); + digits = digits*10 + (unsigned int)(text[i] - '0'); + if (digits > 0) digitCount++; exponent--; } } @@ -1701,16 +1709,19 @@ float TextToFloat(const char *text) } } - if (value != 0.0) + float value = (float)digits; + + if (value != 0.0f) { - double scale = 1.0; - for (int e = (exponent < 0)? -exponent : exponent; (e > 0) && (scale < 1e300); e--) scale *= 10.0; + // 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; } - if (exponent < 0) value /= scale; - else value *= scale; + if ((exponent > 0) && (exponent <= 10)) value *= powersOfTen[exponent]; + else if ((exponent < 0) && (exponent >= -10)) value /= powersOfTen[-exponent]; } - return (float)(value*sign); + return value*sign; } // Copy one string to another, returns bytes copied From b8c18ac47618ec7441c2f8ff257a5a7bc7f524dc Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Wed, 11 Nov 2026 12:57:59 +0530 Subject: [PATCH 3/3] [rtext] Remove static array from `TextToFloat()` --- src/rtext.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/rtext.c b/src/rtext.c index 48a22a9f2dc8..3ce93f6cd4fa 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -1646,9 +1646,6 @@ int TextToInteger(const char *text) // WARNING: Only '.' character is understood as decimal point float TextToFloat(const char *text) { - // Powers of ten a float holds exactly - static const float powersOfTen[11] = { 1e0f, 1e1f, 1e2f, 1e3f, 1e4f, 1e5f, 1e6f, 1e7f, 1e8f, 1e9f, 1e10f }; - // 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; @@ -1717,8 +1714,13 @@ float TextToFloat(const char *text) while ((exponent > 10) && (value < 3.4e38f)) { value *= 1e10f; exponent -= 10; } while ((exponent < -10) && (value > 0.0f)) { value /= 1e10f; exponent += 10; } - if ((exponent > 0) && (exponent <= 10)) value *= powersOfTen[exponent]; - else if ((exponent < 0) && (exponent >= -10)) value /= powersOfTen[-exponent]; + // 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;