2019-11-30 04:16 PM
I have a simple calculate
#define VREF ((uint16_t) 3300)
float X = VREF / 2000;
Why X result is 1? This happen with another calculate like xx00 / xx000
Thanks
Solved! Go to Solution.
2019-11-30 04:57 PM
The first line is useless in this example... The numbers 3300 and 2000 on the second line are interpreted as integers and division is done on integers, which gives the result = 1. If you add ".0", they will be interpreted as double type values, and, if you add ".0f", then as a float type values. That is for constant values, but, if you need to divide integer variables and get float type result, then cast the variables to float type before division.
https://stackoverflow.com/questions/5026570/suffix-of-f-on-float-value
P.S. This is one of the topics, which almost no one cares to explain in their tutorials, university courses etc.
2019-11-30 04:57 PM
The first line is useless in this example... The numbers 3300 and 2000 on the second line are interpreted as integers and division is done on integers, which gives the result = 1. If you add ".0", they will be interpreted as double type values, and, if you add ".0f", then as a float type values. That is for constant values, but, if you need to divide integer variables and get float type result, then cast the variables to float type before division.
https://stackoverflow.com/questions/5026570/suffix-of-f-on-float-value
P.S. This is one of the topics, which almost no one cares to explain in their tutorials, university courses etc.
2019-11-30 05:53 PM
float X = 3300.0f / 2000.0f;
2019-11-30 06:24 PM
sorry, I mean
#define VREF ((uint16_t) 3300)
float X = VREF / 2000;
2019-12-01 03:16 AM
Remember to avoid using float in interrupts routines. more registers to push/pop.