The purpose of INT_MIN/INT_MAX

Get answers to all your basic programming questions. No Ogre questions, please!
N_K
Greenskin
Posts: 115
Joined: Wed Dec 07, 2011 9:05 pm
x 4

The purpose of INT_MIN/INT_MAX

Post by N_K »

Hello all!

I found this code snippet in this forum when looking for a way to define 2d bounding boxes:

Code: Select all

const Vector3* corners = box.getAllCorners();
      int minx, miny, maxx, maxy;
      minx = miny = INT_MAX;
      maxx = maxy = INT_MIN;
      Ogre::Camera* cam = getCamera();
      for(int i = 0 ; i < 7; i++)
      {
         Ogre::Vector2 pt = toRelativeScreenCoordinates(corners[i], cam, winw, winh);

         if(minx > pt.x) minx = pt.x;
         if(miny > pt.y) miny = pt.y;
         if(maxx < pt.x) maxx = pt.x;
         if(maxy < pt.y) maxy = pt.y;
      }
      w = maxx - minx;
      h = maxy - miny;
      x = minx;
      y = miny;
...and I noticed the INT_MIN and INT_MAX datatypes (or are they?). I've seen quite a lot of C/C++ code since I started to learn to code, but honestly, this is the first time I encountered these. Searching for these led me to various C/C++ documentation sites, but all of them described these like:

"INT_MIN: Minimum value for an object of type int. Value: -32767 or less."
"INT_MAX: Maximum value for an object of type int. Value: 32767 or greater."

While stating the obvious, I still found no descriptions/tutorials about the purpose of these, or in which situations are they used.

For example, let's see a part of the code snippet above:

Code: Select all

minx = miny = INT_MAX;
...so here minx equals to miny, which in turn equals to what? 32767? Why is this needed? Taking a wild guess, this is some kind of overflow/underflow protection, but not clear why do we need this in this situation (the code itself gets the positions of the corners of a bounding box, and transforms them to screen space with the external function toRelativeScreenCoordinates).

Would someone be kind enough to explain the purpose of these? (There are _MIN and _MAX for many numeric datatypes, but the principle is the same as for int.)
Thank you in advance.
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: The purpose of INT_MIN/INT_MAX

Post by bstone »

Those are simple constants (defined using macros), not data types, that specify the minimum and maximum values of an int variable. In your C++ code it's better to use the non-macros versions: std::numeric_limits< int >::min() and std::numeric_limits< int >::max().

In the code above they are used to initialize the found maximum and minimum values with the opposite extremes. Nothing to do with overflows/underflows. This way any value that is greater/lower than the initial will replace it and the algorithm will return the actual min/max values from the analyzed data.
N_K
Greenskin
Posts: 115
Joined: Wed Dec 07, 2011 9:05 pm
x 4

Re: The purpose of INT_MIN/INT_MAX

Post by N_K »

bstone wrote:In the code above they are used to initialize the found maximum and minimum values with the opposite extremes.
So in

Code: Select all

minx = miny = INT_MAX;
it will analyze the variables minx and miny, and find the larges value encountered?
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: The purpose of INT_MIN/INT_MAX

Post by bstone »

No, it does that here:

Code: Select all

         if(minx > pt.x) minx = pt.x;
         if(miny > pt.y) miny = pt.y;
         if(maxx < pt.x) maxx = pt.x;
         if(maxy < pt.y) maxy = pt.y;
N_K
Greenskin
Posts: 115
Joined: Wed Dec 07, 2011 9:05 pm
x 4

Re: The purpose of INT_MIN/INT_MAX

Post by N_K »

Then it simply initializes minx and miny with value 32767, and maxx and maxy with -32767, and if any of these variables go beyond/above these limits, they will be "locked" at the initialized values?
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: The purpose of INT_MIN/INT_MAX

Post by bstone »

It's not +/-32767, but -2147483648 and 2147483647 (on most platforms) which are the minimum and the maximum value a 32-bit int variable can represent. And the purpose is not to lock the values inside the range. The idea is to assign the maximum value to minx and miny so at least one of the analyzed values will be less or equal to that. Similar with maxx and maxy. Say you do this instead "minx = miny = 0;", then if your data has all values greater than 100, your discovered minimum values will remain at 0, which is not correct.
N_K
Greenskin
Posts: 115
Joined: Wed Dec 07, 2011 9:05 pm
x 4

Re: The purpose of INT_MIN/INT_MAX

Post by N_K »

Okay, I think I start to get it.

Except this part:
bstone wrote:Say you do this instead "minx = miny = 0;", then if your data has all values greater than 100, your discovered minimum values will remain at 0, which is not correct.
So if both minx and miny are zero, and the data to be analyzed are all greater than 100, then:

Code: Select all

if(0 > 110) minx = 110;
As far as I know, this will never happen as long as the analyzed integer is positive. Why should it be different if it's less than 100? It will be still positive, so this statement still won't happen.

-------------------------------------------------------------------------

But, on the other hand, if minx and miny are INT_MAX, then:

Code: Select all

if(2147483647 > 110) minx = 110;
And if maxx and maxy are INT_MIN, then:

Code: Select all

if(-2147483648 < 110) maxx = 110;
Both of these will basically assign the values held by pt.x and pt.y to minx, miny, maxx, maxy as long as they are between INT_MIN and INT_MAX. If they are not, they will be assigned to the value of INT_MIN and INT_MAX. Is this correct?
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 535

Re: The purpose of INT_MIN/INT_MAX

Post by Kojack »

Both of these will basically assign the values held by pt.x and pt.y to minx, miny, maxx, maxy as long as they are between INT_MIN and INT_MAX.
For the first corner, yes.
The INT_MIN and INT_MAX stuff is just setting the min and max values up to the most extreme wrong value possible, so the first point you then compare them with will always adjust them.

On it's own this is pointless (could have just assigned the values to min and max directly), but this is just the first of 8 points.

When you want to find the smallest value out of a list of numbers, you set your start value to the biggest possible number (that none of the list of numbers can be bigger than). Then for each number in the list, you remember it if it's less than the current one you are remembering.
If they are not, they will be assigned to the value of INT_MIN and INT_MAX. Is this correct?
INT_MIN and INT_MAX are used specifically because the value will never be outside that range. That's the key.


Another way to do this is to set minx, miny, maxx and maxy to the values of the first corner point directly (no INT_MIN or INT_MAX) then loop through corners 1-7 (instead of 0-7). This saves one go through the loop (a few if commands). Not much of a saving, but still better (IMHO).
N_K
Greenskin
Posts: 115
Joined: Wed Dec 07, 2011 9:05 pm
x 4

Re: The purpose of INT_MIN/INT_MAX

Post by N_K »

Okay, I get it now. So instead of an user-created min and max value, they are initialized with the smallest and largest numbers the int datatype can hold on the target system, and they are not mandatory, but convenient. (And that's why they are (and their C++ standard counterparts) rarely used.)

Thank you both for explaining these in a non-cryptic way!