Jump to content
  • Advertisement
Sign in to follow this  
NextEpisode1

Operator precendence

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Both 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 this post


Link to post
Share on other sites
Quote:
Original post by alvaro
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.


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

Share this post


Link to post
Share on other sites
Quote:
Original post by force_of_will
Quote:
Original post by Evil Steve
Off 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 this post


Link to post
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";
}


1
0
Press 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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!