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.

CCS/TMS320F280049: TMS320F280049: problems with accuracy of calculations

Part Number: TMS320F280049


Tool/software: Code Composer Studio

Hello!
I had the following problem in the project, which I noticed a few days ago, but it just now began to bring me real problems. This is a calculation issue. When I assign some number to a float variable, it is misinterpreted by the controller. Those.:
float x1 = 5.3f;
float x2 = 5.3f;
float x3 = 0.0f;

In the Expressions tab, these variables are written as 5,30000019 and then the calculation occurs with these values. For me, this created a real problem when I started to calculate an array of sine values, there, taking into account 500 iterations, the error became very large and the last sine value of 0.006 ... is 0.17.
Tell me, please, what could be the problem?

I also attach the archive with my project.

Project_5.rar

  • It means that 5.3 is not exactly expressible in binary, much like 1/3 is not expressible in base 10.

    The best bet is to do what Kernigham and Plauger recommended in "The Elements of Programming Style" back in 1978.

    "10.0 times 0 .1 is hardly ever 0.1"
    and
    "Working with floating point is like moving sand piles, every time you move one you lose a little sand and pick up a little dirt"

    so, rather than repeatedly summing floating point numbers it is better to use integers and scale them.

    rather then:
    float_num = 0;
    for (int i=0; i<10; i++)
    {
    float_num += 0.1
    }

    try something like:

    for (int i = 0; i<10; i++)
    {
    float_num = i*0.1;
    }

    While any particular number may be a little off, the errors will not accumulate.
  • These variables ...

    Andrey Chernyshev said:
    float x1 = 5.3f;
    float x2 = 5.3f;
    float x3 = 0.0f;

    use IEEE 32-bit single precision format.  If you need more precision, consider using the type long double instead.  That uses IEEE 64-bit double precision format.  If you make that change, then the constants need to be in long double format as well.  Use the suffix L instead of f.  For more detail, please search the C28x compiler manual for the sub-chapter titled C28x double and long double Floating-Point Types.

    Thanks and regards,

    -George

  • Thanks for the clarification, it helped me a lot!