C 中浮点乘法的意外输出

发布于 2024-11-07 03:18:18 字数 1220 浏览 0 评论 0原文

我正在用 C 语言执行乘法和加法运算,但得到了错误的结果。

我正在执行以下操作:

#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

这是我得到的输出:

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

但是正确的答案应该是:

0.0013101

谢谢!

I am performing a multiplication and addition operation in C and am getting the wrong result.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅听莫相离 2024-11-14 03:18:18

尝试 long double,比 float 更精确:
http://en.wikipedia.org/wiki/Long_double

请注意,您应该更改 printf (%Lf 表示长双精度)

Try long double, more precise than float:
http://en.wikipedia.org/wiki/Long_double

Please note that you should change your printf (%Lf for long double)

如果没结果 2024-11-14 03:18:18

我很惊讶它没有崩溃。事实上,我很惊讶它竟然能编译。如果它在没有警告的情况下编译,我会大吃一惊。

  • 没有为 fpreal 和 fpimag 分配内存。
  • fpreal 中的值在初始化之前使用。
  • 使用二维数组访问但不给出任何维度。
  • 将指针 fpreal 和 fpimag 作为浮点数打印出来。
  • 尝试打印出操作前的值,但 fpimag 中的值已被覆盖。

(前两个可能被未显示的代码覆盖 - 其他三个看起来是明确的问题)

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.
  • Values in fpreal are used before being initialised.
  • Uses two dimensional array access but doesn't give any dimensions.
  • Prints out pointers fpreal and fpimag as floats.
  • Tries to print out the values as they were before the operation but value in fpimag has been overwritten.

(The first two may be covered by code not shown - the other three look to be definite problems)

还不是爱你 2024-11-14 03:18:18

看下面两行:

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

你看不出fpimag 和(*fpimag)[行][列]不一样吗? (与 fpreal 相同)您正在编写指向浮点指针值的指针,而不是指针指向的值。试试这个:

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);

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]? (and the same with fpreal) You are writing pointer to float pointer values, not the values the pointers are pointing to. 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);
小镇女孩 2024-11-14 03:18:18

嘿,我发现问题非常明显:

我覆盖了 fpreal 的值,然后尝试在另一个计算中使用它。

因此我做了:

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

我已经更新了代码,以便将 fpreal 和 fpimag 的值存储在一些临时变量中,并使用它们进行计算,如下所示:

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

(*fpreal)[row][column] = oldreal*expreal - oldimag*expimag;
(*fpimag)[row][column] = oldimag*expreal + oldreal*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.

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:

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文