C 中浮点乘法的意外输出
我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试 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)
我很惊讶它没有崩溃。事实上,我很惊讶它竟然能编译。如果它在没有警告的情况下编译,我会大吃一惊。
(前两个可能被未显示的代码覆盖 - 其他三个看起来是明确的问题)
I'm surprised it doesn't crash. In fact I'm surprised it even compiles. I would be gobsmacked if it compiles without warnings.
(The first two may be covered by code not shown - the other three look to be definite problems)
看下面两行:
你看不出fpimag 和(*fpimag)[行][列]不一样吗? (与 fpreal 相同)您正在编写指向浮点指针值的指针,而不是指针指向的值。试试这个:
Look at the following two lines:
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:
嘿,我发现问题非常明显:
我覆盖了 fpreal 的值,然后尝试在另一个计算中使用它。
因此我做了:
我已经更新了代码,以便将 fpreal 和 fpimag 的值存储在一些临时变量中,并使用它们进行计算,如下所示:
谢谢大家的帮助!
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:
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:
Thank you everybody for your assistance though!