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.

Typecast Problem !!!!

Other Parts Discussed in Thread: TMS320F2810

Sir,

I Am Using TMS320F2810, with code composer V5.2 

I am facing a strange problem ,

e.g

long LTemp = 0;

float FTemp  = 5000.0,

LTemp = (long) Ftemp;

here , LTemp is loaded with 4999 instead of 5000,

why Am I Losing a Count ,

is There some Kind of setting I have to do in my compiler,

Please Suggest,

  • Hi Bipin,

    Bipin Patil said:
    LTemp = (long) Ftemp;

    Instead try directly:  LTemp = Ftemp;

    I've done loading float values to integers (in similar fashion) to get rid of decimals previously and they worked like charm for me then.

    Regards,

    Gautam

  • Sir,

    Still facing The Same Problem Losing a Count.
  • Try initializing as:
    float FTemp = 5000.567,
  • sir ,
    Following is the Part of Code in which i am facing Problem. Here I have Hard Coded The Values for Simplicity.

    long TargetPosition =0; //TargetPosition has to be long.. Cant change
    float ShortestPosition = 22.5;
    float Position_factor = (8000/36)
    float temp = 0;

    temp = (float)(Position_factor* ShortestPosition);
    TargetPosition = temp;
  • Just for trial can you try using float datatype for TargetPosition, just to check whether you get 5000.0 or 4999.x
  • Sir,

    Actually I have connected the debugger to 2810 and put a break point at temp,and single stepped the value,
    temp shows 5000.0 in watch window.

    I will try the above try
  • Bipin,

    The problem is with the code line:

         float Position_factor = (8000/36);

    The integer division of 8000 by 36 is done as integer.  The result is an integer, 222.  This is then typecast to a float, 222.0.  This is exactly what the above code is telling the compiler to do, based on order of operations.  When you compute

         temp = (float)(Position_factor* ShortestPosition);
         TargetPosition = temp

    you get temp = 4995.0, and TargetPostion correspondingly is 4995.

    All you have to do is change 8000 and 36 to floating point:

        float Position_factor = (8000.0/36.0);

    Alternately, you can typecast them:

        float Position_factor = ((float)8000/(float)36);

    -------------

    Incidentally, when I tried this:

    long LTemp = 0;
    float FTemp = 5000.0;
    LTemp = (long)FTemp;

    I got LTemp=5000.


    I am using cgtools v6.4.9, but I don't think that would make any difference.  The floating point conversion routines in the RTS library have been the same for years.

    Regards,

    David