I'm trying to implement the following function to be calculated by my STM32
y=0.0006*x^3 - 0.054*x^2 2.9094*x - 2.3578
x
is in range 0 to 215
To avoid the use of pow or any other functions, I have written the following code
static double tmp = 0;
static unsigned short c_m;
static unsigned short c_m_3;
static unsigned short c_m_2;
c_m_3 = c_m*c_m*c_m;
c_m_2 = c_m*c_m;
tmp = (0.0006*c_m_3) - (0.054*c_m_2) (2.9094*c_m) - 2.3578;
dati->sch_g = tmp;
For some reason the calculation is totally wrong as, for istane, if c_m = 1
I should have tmp = 0.4982
instead I got 13
Am I missing something?
CodePudding user response:
short
is 16 bits on all STM32. Thus the value 215 * 215 * 215
will not fit inside one. c_m_3 = c_m*c_m*c_m;
truncates the result as per modulus USHRT_MAX 1
(65536):
Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.
Use uint32_t
instead.
CodePudding user response:
short is only 16 bits, the max value it can hold is 65535. Therefore it will overflow if the number you want to calculate the third power for is over 40. This means that you must use a larger variable type like uint32_t. You can also use ifs to detect overflow for better programming practices.
As another note, it's better to use "uint8_t" and "uint16_t" instead of "unsigned char" and "unsigned short" in embedded programming, because they're more descriptive for the data sizes.