Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / Question about math in C Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Shawn Swift

February 18, 2005, 07:12 PM

I'm trying to translate a program from C, and there seem to be a few bugs I need to track down.

I need to know the following:

If I have this in C:
A -= B*C + D*E

Is that evaluated like this?
A = A - (B*C + D*E)

Or like this?
A = A - B*C + D*E

 
doorman

February 18, 2005, 07:30 PM

int main( void )
{
int A = 5;
int B = 4;
int C = 3;
int D = 2;
int result = 10;

result -= A*B + C*D;

printf( "%in", result );

return 0;
}

ouput = -16



 
Shawn Swift

February 18, 2005, 08:03 PM

So your answer is that it does everything on the right and then subtracts that from the value on the left.

I would have tested it myself, but I don't have a C compiler.

 
theAntiELVIS

February 18, 2005, 10:54 PM

A = A - ((B * C) + (D * E))

1: B is multiplied by C
2: D is multiplied by E
3: Answer 1 is added to answer 2
4: Answer 3 is subtracted from A
5: Answer 4 is assigned to A

Multiplication has higher precedence than addition so it is done first.

 
MonkeyInBlack

February 19, 2005, 06:30 AM

Check out http://www.swansontec.com/sopc.htm

 
This thread contains 5 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.