Public Group

# Operator precendence

This topic is 3037 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i was stunned

int aux =WindowWidth/2;
while ((Center3dX-PointX*GridSize)<(aux))
GridSize--;

works fine
but

while ((Center3dX-PointX*GridSize)<(WindowWidth/2))
GridSize--;
doesnt

im almost sure operator precedence is involved here but i cant get the actual error.Since all operations are right-to-left WindowWidth/2 is the first one to do
it should work the same like the first example

##### Share on other sites
Both samples are equivalent, unless WindowWidth is a float or double.

##### Share on other sites
Or if WindowWidth and GridSize are aliased or otherwise affect each other.

##### Share on other sites
One thing that helps a lot with understanding these kinds of small localized problems is to write just enough code around those lines to make the program runnable. Most of the time you'll realize quickly what's going on by testing on a much easier setting. And if you don't understand it, you can post a tiny complete program here, so we can see for ourselves.

##### Share on other sites
Quote:
 Original post by Evil SteveBoth samples are equivalent, unless WindowWidth is a float or double.

... tricky one:

when i typecast it to (int) it goes smooth but since my variables are:

unsigned short int GridSize ;
int PointX;
const int Center3dX;
const unsigned int WindowWidth;
and 2 is implicitly an const int;

any result of operands is implicitly an int, even if i had no brackets or operator precedence at all (hypothetically speaking)

any methods to check what type is an operation result?
(i could always cout it and see if its a float/int/string , but i would not be able to see if it's a string or a char , signed or unsigned int ,etc)

Off topic :
int/float==int
, right?

##### Share on other sites
Quote:
 Original post by SneftelOr if WindowWidth and GridSize are aliased or otherwise affect each other.

no relationship at all

##### Share on other sites
Quote:
 Original post by alvaroOne thing that helps a lot with understanding these kinds of small localized problems is to write just enough code around those lines to make the program runnable. Most of the time you'll realize quickly what's going on by testing on a much easier setting. And if you don't understand it, you can post a tiny complete program here, so we can see for ourselves.

il keep that in mind ,thanks,but the problem is so simple you usually dont need to compile to see what is does.

##### Share on other sites
Quote:
 Original post by NextEpisode1Off topic :int/float==int, right?

No int/float == float

[Edited by - force_of_will on August 20, 2010 1:03:15 PM]

##### Share on other sites
Quote:
Original post by force_of_will
Quote:
 Original post by Evil SteveOff topic :int/float==int, right?

No int/float == float
For posterity, Evil Steve didn't write that, NextEpisode1 did. I'm sure this was unintentional.

##### Share on other sites
#include <iostream>int main() {	unsigned short int GridSize = 1;	int PointX = 1000;	const int Center3dX = 1;	const unsigned int WindowWidth = 1;	int aux = WindowWidth/2;		std::cout << ((Center3dX-PointX*GridSize)<(aux)) << "\n";	std::cout << ((Center3dX-PointX*GridSize)<(WindowWidth/2)) << "\n";}

10Press any key to continue . . .

In the first case, we're comparing an int to an int. -999 < +1 0 evaluates to true.
In the second case, we're comparing an int to an unsigned int (since WindowsWidth is unsigned and WindowsWidth/2 is also unsigned as a result). As per the C++ standard (emphasis and color added):

Quote:
Cited from ISO/IEC 14882:2003(E), better known as The C++ Standard

# 5 Expressions

[...]

9 Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:
— If either operand is of type long double, the other shall be converted to long double.
— Otherwise, if either operand is double, the other shall be converted to double.
— Otherwise, if either operand is float, the other shall be converted to float.
— Otherwise, the integral promotions (4.5) shall be performed on both operands. 54)
— Then, if either operand is unsigned long the other shall be converted to unsigned long.
— Otherwise, if one operand is a long int and the other unsigned int, then if a long int can rep-
resent all the values of an unsigned int, the unsigned int shall be converted to a long int;
otherwise both operands shall be converted to unsigned long int.
— Otherwise, if either operand is long, the other shall be converted to long.
— Otherwise, if either operand is unsigned, the other shall be converted to unsigned.
[Note: otherwise, the only remaining case is that both operands are int ]

-999 is converted to it's unsigned representation, 4294966297.
4294966297 < 1 0 evaluates to false.

Don't you just love C++? Also, did they yank C4018 from VS2008? I can't get it to give me a proper warning.

(EDIT: And for those wondering, yes, it's applied... from 5.9 Relational operators ¶2: "The usual arithmetic conversions are performed on operands of arithmetic or enumeration type.")

(EDIT: Curse you Zahlman!)

[Edited by - MaulingMonkey on August 22, 2010 2:01:56 AM]

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5
A4L
12

• 11
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633723
• Total Posts
3013542
×