Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Math Problem

Status
Not open for further replies.

crmayer

Programmer
Nov 22, 2002
280
US
struct tmp_state {
char state[3];
int dh_miles;
int b_miles;
int n_loads;
float lhrev;
float lhtotal;
} temp_state[MAXSTATE];

struct tmp_print {
char state[3];
int dh_miles;
float percent_dh;
int n_loads;
int average_dh;
int b_miles;
int average_loh;
float lhrev;
float lhrev_wo_dh;
float lhrev_w_dh;
float lhrev_acc;
float lhrev_acc_wo_dh;
float lhrev_acc_w_dh;
} temp_print[MAXPRINT];

I am trying to do this:

temp_print.percent_dh = (temp_state.lhrev/(temp_state.b_miles+temp_state.dh_miles)*100);


I am not sure what the problem is, not matter how I try this, temp_print.percent_dh is ALWAYS 0.00. I have tried to put b_miles+dh_miles into a total_miles and then divide it by dh_miles, and I still get the same thing, 0.00.

Is there a problem with b_miles and dh_miles being intager type and percent_dh being float????

I have even tried to make percent_dh a char and print the results into percent_dh with sprintf.

Any ideas as to what might be happening here?
 
Code:
temp_state[0].lhrev=100;
temp_state[0].b_miles=5;
temp_state[0].dh_miles=5;

temp_print[0].percent_dh = (temp_state[0].lhrev/(temp_state[0].b_miles+temp_state[0].dh_miles)*100);


//printf("f1 is %f\n",f1);
printf("f1 is %f\n",temp_print[0].percent_dh );
getch();


i think the problem is with the values you are using in there
for the values i put in above, i get a value of 1000.
On a guess, just check that in using i, you are not overrunning array indices
 
> I am not sure what the problem is, not matter how I try this, temp_print.percent_dh is ALWAYS 0.00
Well on the face of it, nothing is wrong.
The usual trap here is doing division in integer arithmetic and wondering why the value is zero, but in your case at least, the division looks like its done as a float.

How are you trying to print it?
Using %f conversion I hope, because trying with %d will give you some very odd answers.

--
 
Optional (but I hope useful;) advice:
Avoid float type in math calculation: usually it's more better using of double (the only point: don't forget %lf specifier in scanf() format string for double arguments).
It seems your 1st post is too abstract: no your problem context...
 
I am trying to print it out to the report like this:

formPrint(lineno, 17, "%6.2f", temp_print.percent_dh;

I have put debugging messages in my code, and that value is not getting assigned. I have printed all the values to the screen before running the calculation and then tried to print the percent_dh after that, and it is always 0.0. It is only this value that is messed up, I am doing division on some other values and they are coming out just fine. The only difference is that they are all float. This paticulare one is to integers being divided then being put into a float.
 
I once had a problem with a C one-liner that confused the compiler. Breaking the calculation into smaller steps and using temporary varaiables was my solution.
Code:
   temp_print[i].percent_dh = (temp_state[i].lhrev/(temp_state[i].b_miles+temp_state[i].dh_miles)*100);
You may want to try rewriting it something like this.
Code:
   float temp = temp_state[i].b_miles;
   temp += temp_state[i].dh_miles;
   temp_print[i].percent_dh  = temp_state[i].lhrev / temp;
   temp_print[i].percent_dh *= 100.0;
 
You need to post a reasonable snippet of code - perferably something which compiles and runs, and demonstrates the problem. That way, we all have something we can try.

Don't forget to mention which OS/Compiler you are using.

Here's another idea - the code in question is mistakenly inside a comment.



--
 
It sounds to me like temp_state.lhrev = 0.0, but if you are sure that the values of the arguments are all correct before the operation, then try:

temp_print.percent_dh = (temp_state.lhrev/((float)[/color red]temp_state.b_miles+temp_state.dh_miles)*100.0[/color red]);


Printing out values is a good way to debug programs that deal with numerical computations. Also, you can try double instead of float.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top