This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Round of and data precision in Cortex-M4

Hi,
I am using STM32F429-Discovery Board (Cortex-M4). When I define/assign the value to any variable/constant ex. #define X 3.14159265 or float32_t Y=0.30945 etc then it changed to X=3.14159274 or Y= 0.309450001 respectively while these variables/constants are used anywhere in the program.
How should I define these variables/constants that it should not be changed?

P.S.
In my code there are some functions which get the inverse of the values (in range of 10^-8 and 10^-9) and it makes a big difference in the result.

Parents Reply Children
  • The initial courses on numerical programming are quite good to take, since it's important to know about the limitations of floating point numbers, stability of algorithms and how they are affected by number of valid value digits etc.

    Double-precision numbers works way better that single-precision numbers but it doesn't matter much with a couple of extra valid digits if used with an algorithm that isn't stable.

    In your case - let the compiler compute 1/x instead of first assigning x to a variable and then compute 1/x. The compiler can compute the inverse with more value digits than a float can store so you end up with just a single round-off error instead of first having one error for assign to x and a second error when the division is assigned to the new variable.