7 + c * -- d / e

L

Les Coover

c = 5, d = -7, e = 11

Could someone explain why 7 + x * -- d / e returns 3.3636 when I expect
it to return -8.7272 which it does

when I use (7 + c) * -- d / e
 
M

Mike Wahler

Les Coover said:
c = 5, d = -7, e = 11

Could someone explain why 7 + x * -- d / e returns 3.3636 when I

Is that 'x' supposed to be 'c'? If not, what
is 'x'? What are the types of 'c', 'd', and 'e'?
expect
it to return -8.7272 which it does

when I use (7 + c) * -- d / e

Read in your favorite C textbook about
operator precedence. I can't tell for
sure since you don't show the types
of the variables, but you may also be
seeing the results of conversions, leading
to unexpected results, e.g. integer division
when you though you were getting floating
point division.

-Mike
 
M

Martin Ambuhl

Les said:
c = 5, d = -7, e = 11

Could someone explain why 7 + x * -- d / e returns 3.3636 when I expect
it to return -8.7272 which it does

when I use (7 + c) * -- d / e

Because you are parsing it incorrectly:

#include <stdio.h>

int main(void)
{
double c = 5, d = -7, e = 11;
printf("7 + c * --d / e: %g\n", 7 + c * --d / e);
c = 5; d = -7; e = 11;
printf("(7 + c) * --d / e: %g\n", (7 + c) * --d / e);
c = 5; d = -7; e = 11;
printf("7 + (c * --d) / e: %g\n", 7 + (c * --d) / e);
c = 5; d = -7; e = 11;
printf("7 + c * (--d / e): %g\n", 7 + c * (--d / e));
return 0;
}

7 + c * --d / e: 3.36364
(7 + c) * --d / e: -8.72727
7 + (c * --d) / e: 3.36364
7 + c * (--d / e): 3.36364
 
L

Lew Pitcher

x is a typo

here is the program

{
float c = 5, d = -7, e = 11, temp;
temp = (7 + c) * -- d / e; /* but why not 7 + c * -- d / e */

If this statement were

temp = 7 + c * -- d / e;

the order of precedence rules would cause it (this particular statement) to be
evaluated
first for unary increments,
next for multiplication and division
finally for addition and subtraction

Thus, the results would be

7 + ( c * (--d)/ e)
according to the rules of C operator precedence evaluation.
printf(" %6f %6f %6f %6f \n\n", temp, c, d, e);
}

I have gone over the level of precedence several times and it seems to me
that adding the prarenthesis
should not change anything, but somehow it does.

--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
 
L

Les Coover

Woops, the reason it did not work is because I was confusing unary plus
precedence with
addition pluss precedence.

c = 5, d = -7, e = 11

7 + c * -- d / e R/L implies d holds -8
7 + c * -- d / e L/R implies (5 * -8) / 11 equivalent to -3.6363
7 + c * -- d / e R/L implies 7 + (-3.6363) = 3.3636

I apologize for the newbe mistake, thank you for the help.
 
E

Eric

Les Coover said:
Woops, the reason it did not work is because I was confusing unary plus
precedence with
addition pluss precedence.

Yes, not always made obvious in various C books in the handy charts they
tend to provide.
 
F

Fred L. Kleinschmidt

Les said:
c = 5, d = -7, e = 11

Could someone explain why 7 + x * -- d / e returns 3.3636 when I expect
it to return -8.7272 which it does

when I use (7 + c) * -- d / e

by precedence, first operation is --d (value: -8)
next: x * -8 (value: -40)
next: -40/11 (value: -3.6363..., assuming types are float or
double)
next: 7 - 3.6363.... (value: 3.3636...)
 
T

The Clap

Steve never puts posting type things in the FAQ. Strictly C questions.
Or anything else for that matter, at least since 1999 or so. *rimshot*

What happened?

Did he shit himself to death when the new standard came out?
 

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

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top