473,499 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

arithmetic precedence head scratcher (noob) C++ VS2005Pro

2 New Member
Here is a snippet of code that returns 0 (c = any number)
d = 5 / 9 * (c-32);

here it works as I expect it should, resulting in the expected numeric value
d = (c-32) * 5 / 9;

I'm spending time reading though MSDN but have only come up with an indication that that (c-32) in the first example might be an empty expression.

Thanks in advance for helping me to understand why the expression gives two different results when the two sides are swapped.
Oct 17 '06 #1
2 1657
dtimes6
73 New Member
(5/9) * (c - 32) != ((c-32) * 5)/9 if c and d are types like int or char.
Oct 17 '06 #2
Banfa
9,065 Recognized Expert Moderator Expert
5 and 9 are integers and / * have the same precedence so it just does the calculation in order

d = 5 / 9 * (c-32);

5 / 9 = 0 (under integer division)

0 * (c-32) = 0 for any c

However here

d = (c-32) * 5 / 9;

the * is first so first it does assuming c = -40

(c-32) = -40 - 32 = -72

-72 * 5 = -360

-360 / 9 = -40



If is normal when doing integer arithmatic to perform multiplication before divison for this reason, it preserves the result and gives better a better result consider the difference between

4321 / 7 * 10 = 6170 (under integer arithmatic)
and
4321 * 10 / 7 = 6172 (under integer arithmatic)

The only thing you have to watch out for is that you don't exceed the limits of you variable type (int in this case) when performing the multiplication.
Oct 17 '06 #3

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

Similar topics

10
6311
by: Newry | last post by:
Hi, If you have both this HTTP header: Content-Type: text/html; charset=ISO-8859-1 and this HTML element in the <head>: <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
25
1667
by: noe | last post by:
Hello, I'm writing a file system filter driver and I've found in an example this sentence: if (VALID_FAST_IO_DISPATCH_HANDLER( fastIoDispatch, FastIoRead )) { return...
21
2999
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
11
1530
by: Rupesh | last post by:
Hello all, See the code .... int i=-3,j=2,k=0,m; m=++i;&&++j||++k; printf ("%d %d %d %d",i,j,k,m); I executed this code on gcc. the o/p i had got is:- -2 3 0 1
6
3989
by: frankh | last post by:
Got earlier very valuable feedback from this group, so I am trying again. Given a string with an arithmetic expression containing variables, e.g. "a+b*sin(x)", how can I calculate its value...
26
3021
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
5
2520
by: junky_fellow | last post by:
Hi, I have a very basic doubt about associativity and precedence of operators. I am not a computer science person and may find it quite weird. Consider an expression 4+5*2
9
3725
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T');...
7
2981
by: pbd22 | last post by:
Hi. I really need some advice on fine-tuning a stored procedure that is the meat of the search logic on my site. Customers are allowed to save searches, which dumps the search logic in a table...
7
3783
by: GCRhoads | last post by:
I'm looking for a very basic high-precision arithmetic library. I need to be able to specify a fixed number of bits or decimal digits (32 decimal digits should be all I need). The only arithmetic...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.