Floating point sum cannot exceed 16777216
Posted: Sun Jun 23, 2024 4:11 pm
In my source code, a float variable is used to hold the integral value of a signal read from A/D. This value remains constant for a long time, so the accumulated value in the float variable is expected to be considerably large.
The problem is that when I reach the value 16777216 the sum cannot go further, and is locked at this value.
I'm using ESP32-WROOM-32D and ESP-IDF 5.2.1.
An example code to reproduce the problem could be:
Isn't the maximum float value 3.402823466E+38 ?
It's very strange, if you change the line test += 0.8f; the sum gets stuck in other values.
If I load a value like 123456890.0f into the float variable, it will represent something like 123456792.0f, which I'm okay with. Why when I make successive sums the float variable can't exceed the value 16777216?
Is it some hardware configuration or some limitation of the ESP32 or am I forgetting some understanding about the floating point representation?
The problem is that when I reach the value 16777216 the sum cannot go further, and is locked at this value.
I'm using ESP32-WROOM-32D and ESP-IDF 5.2.1.
An example code to reproduce the problem could be:
Code: Select all
float test = 0.0f;
int i = 0;
while (test < 123456789.0f)
{
test += 0.8f;
if (++i > 100000)
{
ESP_LOGI(TAG, "test=%0.2f", test);
i = 0;
}
}
It's very strange, if you change the line test += 0.8f; the sum gets stuck in other values.
If I load a value like 123456890.0f into the float variable, it will represent something like 123456792.0f, which I'm okay with. Why when I make successive sums the float variable can't exceed the value 16777216?
Is it some hardware configuration or some limitation of the ESP32 or am I forgetting some understanding about the floating point representation?