473,320 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Order of Evaluation

1
Hi,
Yesterday in class my lecturer was discussing order of evaluation using the example of converting Fahrenheit to Celsius.

He gave an example of tempC = (tempF - 32)*5/9 where tempC and tempF are floats.

He stated that this works because the operations of equal precedance are evaluated left to right so when the float result of tempF - 32 is multiplied by 5, 5 is first cast to a float, and then when that result is divided by 9 the 9 is first cast to a float and the result is as expected.

He also said that if the expression was rewritten as tempC = 5/9*(tempF-32) the answer will be 0 because 5/9 is evaluated first, treating 5 and 9 as ints and thus producing 0, which is then cast to a float and multiplied by (tempF - 32) which will give a final result of 0. Indeed, compiling and running these examples (using gcc) confirms that the first method gives a correct result and the second gives 0.

I understand everything that he said and what's going on, but I'm not sure if his statement that operations of equal precedence are evaluated left to right is correct. Clearly it is the case for the implementation of that compiler but is there anything saying it has to be done in that order or only that the order must be consistent within that implementation?

Thanks,
Justin
Jul 24 '07 #1
6 2219
JosAH
11,448 Expert 8TB
This is what the C99 Standard has to say about it:

[#3] The grouping of operators and operands is indicated by
the syntax.61) Except as specified later (for the function-
call (), &&, ||, ?:, and comma operators), the order of
evaluation of subexpressions and the order in which side
effects take place are both unspecified.
Note though that both the * and / operators are left associative, so a/b/c is
evalutated as (a/b)/c instead of a/(b/c) (e.g. 8/4/2 == (8/4)/2 == 1 != 8/(4/2)).
The same holds for the * operator where the associativity doesn't matter.

It coincides nicely with the Celcius-Fahrenheit expression, i.e. the casts are
performed 'just right' but expressions don't evaluate from left to right.
Here's a counter example: a*b-c*d-e*f; e.g. e*f may be evaluated first,
c*d next and a*b last before the last two terms are subracted from the first.

kind regards,

Jos
Jul 24 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Also read up on arithmetic conversions for the operands of an operator:

The general rule is:
1) any operands of type char or type short are converted to int
2) any operands of type float are converted to double
Then:
1) if either operand is a double, the other is converted to double and the result is double
2) Else, if either operand is long, the other is ocnverted to long and the result is long
3) Else, if either operand is unsigned, the other is converted tio unsigned and the result is unsigned
4) Else, both operands are int and the result is int
Jul 24 '07 #3
JosAH
11,448 Expert 8TB
Also read up on arithmetic conversions for the operands of an operator:

The general rule is:
1) any operands of type char or type short are converted to int
2) any operands of type float are converted to double
Then:
1) if either operand is a double, the other is converted to double and the result is double
2) Else, if either operand is long, the other is ocnverted to long and the result is long
3) Else, if either operand is unsigned, the other is converted tio unsigned and the result is unsigned
4) Else, both operands are int and the result is int
All true but compare the value of 5/9*18.0 against 18.0*5/9

kind regards,

Jos
Jul 24 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
OK.

5/9*18.0

is evaluated 5/9 result is an int of 0.
Evaluation stops. The expression is false.
Result is 0.

18.0*5/9

Here 18.0*5 converts 5 to a double. Result is 90.0.
Then 90.0/9 converts 9 to a double. Result is 10.0
Jul 24 '07 #5
JosAH
11,448 Expert 8TB
OK.

5/9*18.0

is evaluated 5/9 result is an int of 0.
Evaluation stops. The expression is false.
Result is 0.
Minor nitpick: the evaluation doesn't stop, e,g, in the expression 5/9*f(42)
the function call f(42) *is* evaluated. Shortcuts only apply for the && and ||
operators. Even more: f(42) might have been evaluated *before* 5/9 is evaluated.
The order of evaluation of sub-expressions is undetermined so the OP's teacher
is dead wrong (too sad).

kind regards,

Jos
Jul 24 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
Shortcuts only apply for the && and ||
operators.
.

Ah. Yes, I will remember that. Thank you.
Jul 25 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
8
by: der | last post by:
Hello all, I've a question about order of evaluations in expressions that have && and || operators in them. The question is: will the evalution go left-to-right, no matter what -- even if the...
13
by: Richard | last post by:
Boy, I'll sure bet this is a FAQ. Many years ago, my "runtime behavior of programming languages" prof absolutely guaranteed that C parameters are evaluated left-to-right. He was a bright guy...
4
by: Frank Wallingford | last post by:
Note: For those with instant reactions, this is NOT the common "why is i = i++ not defined?" question. Please read on. I came across an interesting question when talking with my colleagues....
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
9
by: John Smith | last post by:
I've been playing with splint, which returns the following warning for the code below: statlib.c: (in function log_norm_pdf) statlib.c(1054,31): Expression has undefined behavior (left operand...
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.