I'm Incrementing float variable in ISR by 0.0001. It works well up to 2048 and after that it stops. Compiler optimization is OFF. Variable declared as volatile(Removing volatile results the same). Please suggest solution.
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
I'm Incrementing float variable in ISR by 0.0001. It works well up to 2048 and after that it stops. Compiler optimization is OFF. Variable declared as volatile(Removing volatile results the same). Please suggest solution.
A 32-bit (single precision) only has 24 bits of mantissa.
Looking at IEEE-754 Floating-Point Conversion if enter 2047.9999 as the decimal value then both the single precision and double precision floating point values are 2047.9999:
Whereas if enter 2048.0001 the single precision value is 2048.0000 and the double precision value is 2048.0001:
I.e. a single precision floating point value can't represent the result of 2048 + 0.0001
Please suggest solution.
One possible solution would be to change the variable from single (32-bits) to double (64-bits), but that will be slower as the TMS320F28379D only has hardware single precision floating point instructions, and double precision has to be performed in software.
What is the floating point variable which is being incremented by 0.0001 in the ISR being used to control, and could it be changed to a (fixed point) integer?