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.

宏定义带来计算错误

Other Parts Discussed in Thread: TMS320C6678

    大家好!

   我使用的TMS320C6678评估板,我使用了如下的宏定义:

    fun.h

    #define WEIGHT_COFF 0.3

 在main()函数调用的子函数中,我使用WEIGHT_COFF,如下:

    unsigned char* pDetailResult = (unsigned char*)0x0C0A0000; 

    unsigned char* pGuassianResult = (unsigned char*)0x0C0B4000;

    for(i = 0; i<256; i++)

    {

       for(j = 0;j<52; j++)

       {

           pGuassianResult[i*52+j] =(1-WEIGHT_COFF)*(pGuassianResult[i*52+j])+WEIGHT_COFF*(pDetailResult[i*52+j]);

       }

    }

保证地址区间没有覆盖,但是如上的pGuassianResult计算结果,不同于将WEIGHT_COFF直接写成0.3的计算结果,为什么啊?

  • pGuassianResult[]和pDetailResult[]都是 unsigned char类型,你可以显式的把它们转成float类型试试,例如:

    pGuassianResult[i*52+j] =(unsigned char)((1-WEIGHT_COFF)*(float)(pGuassianResult[i*52+j])+WEIGHT_COFF*(float)(pDetailResult[i*52+j]));

    另外宏定义建议加括号,例如

        #define WEIGHT_COFF 0.3  写成

        #define WEIGHT_COFF (0.3) 比较好