考虑角点情况如何做定点乘法

时间:2014-03-23 09:49:58

标签: c

我是C语言的新手。我想问一下如何实现C函数来乘以两个8位有符号定点数。函数还应该检查Signed x Signed multiply的角点情况。将乘法结果截断为8位。当我使用带角点的乘法函数时,它会产生以下错误

  

错误C2224:左边的' .real'必须有struct / union类型       IntelliSense:表达式必须具有类类型
      错误C2224:左边的' .imag'必须有struct / union类型

因为我是C的新手,所以我无法完全理解错误消息。这是我的代码,我试图写。无法解决它:

typedef struct COMPLEX 
{
    short real;
    short imag;
}COMPLEX;   

COMPLEX ComplexMultFixed(COMPLEX z1, COMPLEX z2); 

char multiplied(char z1, char z2)    
{
    short  c;
    int i,j;
    short   L1,L2;
    char ans;
    COMPLEX out;
    int overflow;
    c = z1 * z2;

    if (c != 0x4000)
    {
        c *=2;
    }
    else
    {
        overflow = 1;
        c  = 0x7fff;
    }

    L1 = z1.real * z2.real;
    L2 = z1.imag * z2.imag;     // Rounding and truncation
    out.real = (((L1 - L2)+0x0040)>>7);
    L1 =  z1.real * z2.imag;
    L2 =  z1.imag * z2.real;
    // Rounding and truncation
    out.imag = (((L1 + L2)+0x0040)>>7);
    return(ans);
}

1 个答案:

答案 0 :(得分:1)

似乎以下两个函数存在一些混淆。

COMPLEX ComplexMultFixed(COMPLEX z1, COMPLEX z2); 

char multiplied(char z1, char z2)    
{

它们对你来说一样吗?对我来说它应该是一样的..

将类型从char更改为COMPLEX

COMPLEX multiplied(COMPLEX z1, COMPLEX z2)