Working with large floating point numbers.

lordreas
Posts: 1
Joined: Tue Mar 14, 2023 11:35 pm

Working with large floating point numbers.

Postby lordreas » Tue Mar 14, 2023 11:50 pm

Hello there,

I'm struggling with calculations involving large floats. Running the following code on my Wemos Lolin32 Lite on Arduino featuring an ESP-32 Rev1
  1. double test = 2.;
  2.  
  3. void setup() {
  4.   // put your setup code here, to run once:
  5.   Serial.begin(31250);
  6. }
  7.  
  8. void loop() {
  9.   // put your main code here, to run repeatedly:
  10.   test*=1.1;
  11.   Serial.println(test);
  12. }
leading to the following output after some time:

Code: Select all

3400938557.93
3741032413.72
4115135655.09
ovf
ovf
ovf
4115135655.09 is way below theoretical limits of float (3e38) let alone double (1e308). What's happening here? I know Esp32 is not the best number for fast or precise FLOPs, but something else but precision seems to be wrong here.

I would be glad if someone could reproduce this / help me / hint me at other resources, thanks in advance!

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: Working with large floating point numbers.

Postby ESP_Sprite » Wed Mar 15, 2023 12:19 am

That overflow seems to happen at the limit of a 32-bit integer. I'd guess the double variable itself doesn't overflow; it's the println routine that messes up. Perhaps you can see what happens if you replace that with a printf("%f\n", test); ?

Who is online

Users browsing this forum: No registered users and 52 guests