简体   繁体   English

浮点乘法的C输出意外

[英]Unexpected output in C from float multiply

I am performing a multiplication and addition operation in C and am getting the wrong result. 我在C中执行乘法和加法运算,并得到错误的结果。

I am performing the following operation: 我正在执行以下操作:

#include <stdio.h>
#include <strlib.h>
#include <math.h>

int main() 
{
    float** fpimag;
    float** fpreal;

    // Some code to assign fpimag and fpreal
    // fpimag and fpreal are two dimensional arrays that are
    // passed by reference to some functions to assign data to them
    allocatememory(&fpimag,&fpreal);
    storedata(&fpimag,&fpreal);


    autofunction(&fpimag,&fpreal);
} // main()

void autofunction(float*** fpimag,float***fpreal) {
    float expreal;
    float expimag;

    expreal = cos((*afph_correct)[row][column]);
    expimag = sin((*afph_correct)[row][column]);

    (*fpimag)[row][column] = (*fpimag)[row][column] * expreal + (*fpreal)[row][column] * expimag;

 printf("Operation looks like this\n");
 printf("%f*%f + %f*%f\n",(*fpimag)[row][column],expreal,(*fpreal)[row][column],expimag);
 printf("The value is %f\n",(*fpimag)[row][column]);
} // autofunction

This is the output I get: 这是我得到的输出:

Operation looks like this
-0.003095*-0.431162 + 0.000027*-0.902275
The value is 0.003865

However the correct answer should be: 但是正确的答案应该是:

0.0013101

Thank you! 谢谢!

Try long double, more precise than float: http://en.wikipedia.org/wiki/Long_double 尝试长双精度,比浮点精度更精确: http : //en.wikipedia.org/wiki/Long_double

Please note that you should change your printf (%Lf for long double) 请注意,您应该更改您的printf(长整数为%Lf)

I'm surprised it doesn't crash. 我很惊讶它没有崩溃。 In fact I'm surprised it even compiles. 实际上我很惊讶它甚至可以编译。 I would be gobsmacked if it compiles without warnings. 如果它编译时没有警告,我将很震惊。

  • No memory is allocated for fpreal and fpimag. 没有为fpreal和fpimag分配内存。
  • Values in fpreal are used before being initialised. fpreal中的值在初始化之前使用。
  • Uses two dimensional array access but doesn't give any dimensions. 使用二维数组访问,但不提供任何维。
  • Prints out pointers fpreal and fpimag as floats. 以浮点形式输出指针fpreal和fpimag。
  • Tries to print out the values as they were before the operation but value in fpimag has been overwritten. 尝试打印出与操作前相同的值,但fpimag中的值已被覆盖。

(The first two may be covered by code not shown - the other three look to be definite problems) (前两个可能被未显示的代码覆盖-其他三个看起来是确定的问题)

Look at the following two lines: 请看以下两行:

(*fpimag)[row][column] = (*fpimag)[row][column] * expreal +
                         (*fpreal)[row][column] * expimag;
printf("%f*%f + %f*%f\n",fpimag,expreal,fpreal,expimag);

Can't you see that fpimag is not the same as (*fpimag)[row][column]? 您看不到fpimag与(* fpimag)[row] [column]不同吗? (and the same with fpreal) You are writing pointer to float pointer values, not the values the pointers are pointing to. (与fpreal相同),您正在将指针写为浮点指针值,而不是指针所指向的值。 Try this instead: 尝试以下方法:

float imag = (*fpimag)[row][column];
float real = (*fpreal)[row][column];
(*fpimag)[row][column] = imag * expreal + real * expimag;
printf("%f*%f + %f*%f\n",imag,expreal,real,expimag);

Hey I found the problem it was pretty obvious: 嘿,我发现问题很明显:

I was overwriting the value of fpreal and then trying to use it in another calculation. 我正在覆盖fpreal的值,然后尝试在其他计算中使用它。

Thus I did: 因此,我做到了:

(*fpreal)[row][column] = (*fpreal)[row][column]*expreal - (*fpimag)[row][column]*expimag;
(*fpimag)[row][column] = (*fpimag)[row][column]*expreal + (*fpreal)[row][column]*expimag;

I have since updated the code so that the values of fpreal and fpimag are stored in some temporary variables and use those for the calculations, as such: 此后,我更新了代码,以便将fpreal和fpimag的值存储在某些临时变量中,并将其用于计算,如下所示:

oldreal = (*fpreal)[row][column];
oldimag = (*fpimag)[row][column];

(*fpreal)[row][column] = oldreal*expreal - oldimag*expimag;
(*fpimag)[row][column] = oldimag*expreal + oldreal*expimag;

Thank you everybody for your assistance though! 谢谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM