Unexpected output in C from float multiply
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 b开发者_如何转开发e:
0.0013101
Thank you!
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.
- 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)
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);
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!
精彩评论