Thursday, 15 March 2012

c - Subtraction between static unsigned long variables -



c - Subtraction between static unsigned long variables -

i have problem subtraction between 2 static unsigned long variables.

my variables defined follows:

static unsigned long actual_value; static unsigned long incoming; static unsigned long outgoing;

the operation in while cycle is:

actual_value = actual_value - (outgoing - incoming) / 1000;

where "outgoing" > "incoming". problem actual_value doesn't alter @ each iteration. never modify value of "actual_value" in other point of while cycle, problem here.

in fact, if seek substituting equation with:

actual_value = actual_value - 1;

correctly @ each iteration value decreases 1.

however, if alter with:

actual_value = actual_value - 0.1;

again @ each iteration value decreases 1. think have not understand how static unsigned variables work.

where's problem?

thanks in advance.

based on response yo comment - outgoing - incoming less 1000, result of (outgoing - incoming) / 1000 0.

the solution depends on need. if want real value division, this:

( outgoing - incoming ) / 1000.0

which same as

( outgoing - incoming ) / 1000.

then floating point number, between 0 , 1.

but want achieve? if ( outgoing - incoming ) < 1000 , not 0, code decrease value of actual_value 1, actual_value unsigned long. if , if outgoing - incoming 0, actual_value not changed. need?

based on comments below, need float or double type actual_value. , then, utilize actual_value -= ( outgoing - incoming ) / 1000.;

if want print nearest integer value actual_value, need

printf( "%ul\n", (unsigned long)( actual_value + .5 ) );

c

No comments:

Post a Comment