expression eval question

K

kilgore

Hi all,

I have encountered a perplexing expression evaluation. In the code
below, the output of the 'ref' printf statement is
3.501 and the output of the 'comp' printf statement is 0.034. I do not
understand where the factor of 100 is coming from. Any help
appreciated.
Thanks!


#include <stdio.h>
typedef unsigned short word;
int main ( int argc, char ** argv)
{
float s; /* span value */
float z; /* zero value */
float u;
float d1;
float out1;
float out2;
word gain;
word bc;

u = 2765;
bc = 4096;
s = 20;
z = -10;
gain = 1;

printf("u = %f\n",u);
printf("s = %f\n",s);
printf("z = %f\n",z);
printf("s = %f\n",s);
d1 = (float) bc * s + z; // why is this NOT the same as the middle
term in the 'out1' expression below?
printf("d1 = %f\n",d1);
out1 = (u/(float)bc * s + z)/(float) gain;
out2 = (u/d1)/gain;
printf(" ref: %2.3f out1 = %2.3f\n",(u / (float)bc * s + z) /
(float)gain, out1);
printf(" comp: %2.3f out2 = %2.3f\n",(u/d1)/(float) gain, out2);
}

The output is:
u = 2765.000000
s = 20.000000
z = -10.000000
s = 20.000000
d1 = 81910.000000
ref: 3.501 out1 = 3.501
comp: 0.034 out2 = 0.034
 
G

Guest

kilgore said:
Hi all,

I have encountered a perplexing expression evaluation. In the code
below, the output of the 'ref' printf statement is
3.501 and the output of the 'comp' printf statement is 0.034. I do not
understand where the factor of 100 is coming from. Any help
appreciated.
Thanks!


#include <stdio.h>
typedef unsigned short word;
int main ( int argc, char ** argv)
{
float s; /* span value */
float z; /* zero value */
float u;
float d1;
float out1;
float out2;
word gain;
word bc;

u = 2765;
bc = 4096;
s = 20;
z = -10;
gain = 1;

printf("u = %f\n",u);
printf("s = %f\n",s);
printf("z = %f\n",z);
printf("s = %f\n",s);
d1 = (float) bc * s + z; // why is this NOT the same as the middle
term in the 'out1' expression below?

// comments have been made standard C, but they're not a good idea for
posting code on any system where line breaks may be inserted without
your knowledge. This is why.
printf("d1 = %f\n",d1);
out1 = (u/(float)bc * s + z)/(float) gain;
out2 = (u/d1)/gain;

a/b * c + d is not a/(b * c + d). :)
 
K

kilgore

a/b * c + d is not a/(b * c + d). :)

OF COURSE -- DOH! I'm embarrassed for a) having posted this, and b)
for not having seen this...
perhaps more coffee will help.

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,414
Latest member
djangoframe

Latest Threads

Top