Demonstration of STM32F series floating point math precision

Post your cool example code here.
Post Reply
AndrewBCN
Posts: 105
Joined: Sun Apr 25, 2021 3:50 pm
Answers: 1
Location: Strasbourg, France

Demonstration of STM32F series floating point math precision

Post by AndrewBCN »

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:

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);
}
And here is what the code above prints on the serial monitor:

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
Looking good!

Suggested reading:
AN4044 Application note
Floating point unit demonstration on STM32 microcontrollers

https://www.st.com/resource/en/applicat ... ronics.pdf
Last edited by AndrewBCN on Sat May 29, 2021 9:01 pm, edited 1 time in total.
mlundin
Posts: 94
Joined: Wed Nov 04, 2020 1:20 pm
Answers: 6
Location: Sweden

Re: Demonstration of STM32F series floating point math precision

Post by mlundin »

Thats double, 64 bits with range 2.22507385850720138e-308 to 1.79769313486231571e+308 (normalized values) that is seventeen decimal place approx and its not hardware accelerated, so good for accuracy with limited use.
The STM32F4 (CortexM4F) has single precision hardware float with range 1.175494351e-38 to 3.40282347e+38 (normalized values)
AndrewBCN
Posts: 105
Joined: Sun Apr 25, 2021 3:50 pm
Answers: 1
Location: Strasbourg, France

Re: Demonstration of STM32F series floating point math precision

Post by AndrewBCN »

mlundin wrote: Sat May 29, 2021 10:30 am Thats double, 64 bits with range 2.22507385850720138e-308 to 1.79769313486231571e+308 (normalized values) that is seventeen decimal place approx and its not hardware accelerated, so good for accuracy with limited use.
The STM32F4 (CortexM4F) has single precision hardware float with range 1.175494351e-38 to 3.40282347e+38 (normalized values)
Indeed, it's good to point out that for single precision (type float) and for most (but not all) mathematical calculations, the compiler will generate code for the STM32F4 series that will directly use its single precision floating point hardware, but for double precision the compiler will generate code that uses NewLib/NewLib-Nano routines for double precision calculations.
Conversely, for the STM32F7 series MCUs that have both single and double precision floating point hardware, gcc will always try to directly use the floating point hardware.

I found this article very informative:
10 useful tips for using the floating point unit on the Cortex-M4
https://community.arm.com/developer/ip- ... -processor

So again my main point: if you are worried that your STM32 processor may or may not calculate with the required precision whatever you are tasking it with, nothing better than to write some code snippets and check the results, as in the example I provided.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Demonstration of STM32F series floating point math precision

Post by ag123 »

this old article is quite interesting
https://docs.oracle.com/cd/E19957-01/80 ... dberg.html
https://docs.oracle.com/cd/E19957-01/80 ... 0-7895.pdf
https://www.itu.dk/~sestoft/bachelor/IE ... rticle.pdf

it kind of say that multiplying and subtracting two nearby numbers can introduce large errors
e.g. b^2 - 4ac
i'd normally simply use doubles and ignore them, that's ok on say a pc or a good processor but is expensive on mcu, e.g. on f4 losing the use of fpu.
i'd guess one'd need to be a little more careful while using float vs double. my guess is one easy way is to do the same set of calcs in double then using floats and examine if large errors occur.
multiplication and subtraction probably matter e.g. in use cases like control where one measures the differences to do feedback.
then in (inverse) kinematics say a 3d printer to calculate the positions to move on a delta 3d printer.
for more 'straight forward' cases, we'd probably tolerate the rounding, precision loss errors, but probably not all apps
Post Reply

Return to “Code snippets”