Demonstration of STM32F series floating point math precision
Posted: Sat May 29, 2021 9:31 am
Hello,
Let's assume that you are using an STM32F series MCU with hardware floating point and are concerned about precision, rounding errors, etc. I was confronted with such worries for my STM32 GPSDO project. The best way to remove any uncertainty is to try some test code, below is some test code that I used to confirm that the STM32F411CEU6 and the gcc compiler were doing what I wanted them to do:
And here is what the code above prints on the serial monitor:
Looking good!
Suggested reading:
AN4044 Application note
Floating point unit demonstration on STM32 microcontrollers
https://www.st.com/resource/en/applicat ... ronics.pdf
Let's assume that you are using an STM32F series MCU with hardware floating point and are concerned about precision, rounding errors, etc. I was confronted with such worries for my STM32 GPSDO project. The best way to remove any uncertainty is to try some test code, below is some test code that I used to confirm that the STM32F411CEU6 and the gcc compiler were doing what I wanted them to do:
Code: Select all
/* Floating point math test on the STM32
* Do I need to worry?
*/
// Some global variables
double dbten=0, dbhun=0, dbtho=0;
double dbtth=0;
// uint64_t x1, x2;
// uint64_t y1, y2;
// uint64_t z1, z2;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); // USB serial
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(F("Floating point on STM32, precision test"));
// One decimal
uint64_t x1 = 200000123;
uint64_t x2 = 100000120;
dbten = double(x1 - x2) / 10.0;
Serial.println(F("One decimal test"));
Serial.println(dbten, 1);
// Two decimals
uint64_t y1 = 2000000123;
uint64_t y2 = 1000000100;
dbhun = double(y1 - y2) / 100.00;
Serial.println(F("Two decimals test"));
Serial.println(dbhun, 2);
// Three decimals
uint64_t z1 = 20000000123;
uint64_t z2 = 10000000000;
dbtho = double(z1 - z2) / 1000.000;
Serial.println(F("Three decimals test"));
Serial.println(dbtho, 3);
// Four decimals
uint64_t w1 = 200000001234;
uint64_t w2 = 100000000000;
dbtth = double(w1 - w2) / 10000.0000;
Serial.println(F("Four decimals test"));
Serial.println(dbtth, 4);
delay(5000);
}
Code: Select all
Floating point on STM32, precision test
One decimal test
10000000.3
Two decimals test
10000000.23
Three decimals test
10000000.123
Four decimals test
10000000.1234
Suggested reading:
AN4044 Application note
Floating point unit demonstration on STM32 microcontrollers
https://www.st.com/resource/en/applicat ... ronics.pdf