473,544 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

min and max running values

Hi

often I need to read numbers and only keep the highest or lowest.
so I do something like
int uLimit = 0;
int lLimit = 999999999999; //hoping the compiler will not complain

uLimit = val_read uLimit ? val_read : uLimit;
lLimit = val_read < lLimit ? val_read : lLimit;

how do I choose the original lLimit?
is there more cleaver way?

thanks
Mar 5 '07 #1
41 2728
Gary Wessle wrote:
Hi

often I need to read numbers and only keep the highest or lowest.
so I do something like
int uLimit = 0;
'int' can be negative, you know...
int lLimit = 999999999999; //hoping the compiler will not complain
Why not just

int lLimit = INT_MAX; // or use std::numeric_li mits<int>::max

?
>
uLimit = val_read uLimit ? val_read : uLimit;
lLimit = val_read < lLimit ? val_read : lLimit;

how do I choose the original lLimit?
is there more cleaver way?
Clever? I don't know. Reliable more like it.

V
-
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 5 '07 #2
On 2007-03-05 10:22:49 -0800, Gary Wessle <ph****@yahoo.c omsaid:
Hi

often I need to read numbers and only keep the highest or lowest.
so I do something like
int uLimit = 0;
What about negative values?
int lLimit = 999999999999; //hoping the compiler will not complain
Why "hope"? Why not just get the value right to begin with?
>
uLimit = val_read uLimit ? val_read : uLimit;
lLimit = val_read < lLimit ? val_read : lLimit;

how do I choose the original lLimit?
#include <limits>
#include <algorithm>

....

int uLimit = std::numeric_li mits<int>::min( );
int lLimit = std::numeric_li mits<int>::max( );

uLimit = std::max(val_re ad, uLimit);
lLimit = std::min(val_re ad, uLimit);
is there more cleaver way?

thanks

--
Clark S. Cox III
cl*******@gmail .com

Mar 5 '07 #3
how do I choose the original lLimit?
is there more cleaver way?
use std::numeric_li mits<int>::max( ) and
std::numeric_li mits<int>::min( ).

Mar 5 '07 #4
Gary Wessle wrote:
Hi

often I need to read numbers and only keep the highest or lowest.
so I do something like
int uLimit = 0;
int lLimit = 999999999999; //hoping the compiler will not complain

uLimit = val_read uLimit ? val_read : uLimit;
lLimit = val_read < lLimit ? val_read : lLimit;

how do I choose the original lLimit?
Use the first value of val_read for both limits.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Mar 5 '07 #5
On 2007-03-05 15:31:29 -0800, Pete Becker <pe**@versatile coding.comsaid:
Gary Wessle wrote:
>Hi

often I need to read numbers and only keep the highest or lowest.
so I do something like
int uLimit = 0;
int lLimit = 999999999999; //hoping the compiler will not complain

uLimit = val_read uLimit ? val_read : uLimit;
lLimit = val_read < lLimit ? val_read : lLimit;

how do I choose the original lLimit?

Use the first value of val_read for both limits.
I would tend to disagree, as that would require a special case for the
first iteration of the loop.

i.e. I would prefer A to B:

/*A*/
int maxValue = std::numeric_li mits<int>::min( );
int minValue = std::numeric_li mits<int>::max( );

while( ... )
{
int input = getNextValue();
maxValue = std::max(maxVal ue, input);
minValue = std::min(minVal ue, input);

...
}

/*B*/
int input = getNextValue();
int maxValue = input;
int minValue = input;

do
{
input = getNextValue();
maxValue = std::max(maxVal ue, input);
minValue = std::min(minVal ue, input);
}
while(...);

--
Clark S. Cox III
cl*******@gmail .com

Mar 6 '07 #6
Clark Cox wrote:
On 2007-03-05 15:31:29 -0800, Pete Becker <pe**@versatile coding.comsaid:
>Gary Wessle wrote:
>>Hi

often I need to read numbers and only keep the highest or lowest.
so I do something like
int uLimit = 0;
int lLimit = 999999999999; //hoping the compiler will not complain

uLimit = val_read uLimit ? val_read : uLimit;
lLimit = val_read < lLimit ? val_read : lLimit;

how do I choose the original lLimit?

Use the first value of val_read for both limits.

I would tend to disagree, as that would require a special case for the
first iteration of the loop.

i.e. I would prefer A to B:
Err, they do two different things. And, of course, they're written to
make A look better.
/*A*/
int maxValue = std::numeric_li mits<int>::min( );
int minValue = std::numeric_li mits<int>::max( );

while( ... )
{
int input = getNextValue();
maxValue = std::max(maxVal ue, input);
minValue = std::min(minVal ue, input);

...
}

/*B*/
int input = getNextValue();
int maxValue = input;
int minValue = input;

do
{
input = getNextValue();
maxValue = std::max(maxVal ue, input);
minValue = std::min(minVal ue, input);
}
while(...);
Let me present two versions that are biased the other way:

/*A*/
maxValue = std::numeric_li mits<int>::min( );
minValue = std::numeric_li mits<int>::max( );

while (...)
{
int input = getNextValue();
maxValue = std::max(maxVal ue, input);
minValue = std::min(minVal ue, input);
....
}

/*B*/
maxValue = minValue = getNextValue();

while (...)
{
int input = getNextValue();
maxValue = std::max(maxVal ue, input);
minValue = std::min(minVal ue, input);
....
}

Obviously B is vastly superior to the wordy, verbose, lengthy,
overwrought, wordy, redundant, and repetitive A. <g>

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Mar 6 '07 #7
On Mar 5, 5:33 pm, Pete Becker <p...@versatile coding.comwrote :
Clark Cox wrote:
On 2007-03-05 15:31:29 -0800, Pete Becker <p...@versatile coding.comsaid:
Gary Wessle wrote:
Hi
>often I need to read numbers and only keep the highest or lowest.
so I do something like
int uLimit = 0;
int lLimit = 999999999999; //hoping the compiler will not complain
>uLimit = val_read uLimit ? val_read : uLimit;
lLimit = val_read < lLimit ? val_read : lLimit;
>how do I choose the original lLimit?
Use the first value of val_read for both limits.
I would tend to disagree, as that would require a special case for the
first iteration of the loop.
i.e. I would prefer A to B:

Err, they do two different things. And, of course, they're written to
make A look better.
/*A*/
int maxValue = std::numeric_li mits<int>::min( );
int minValue = std::numeric_li mits<int>::max( );
while( ... )
{
int input = getNextValue();
maxValue = std::max(maxVal ue, input);
minValue = std::min(minVal ue, input);
...
}
/*B*/
int input = getNextValue();
int maxValue = input;
int minValue = input;
do
{
input = getNextValue();
maxValue = std::max(maxVal ue, input);
minValue = std::min(minVal ue, input);
}
while(...);

Let me present two versions that are biased the other way:

/*A*/
maxValue = std::numeric_li mits<int>::min( );
minValue = std::numeric_li mits<int>::max( );

while (...)
{
int input = getNextValue();
maxValue = std::max(maxVal ue, input);
minValue = std::min(minVal ue, input);
...

}

/*B*/
maxValue = minValue = getNextValue();

while (...)
{
int input = getNextValue();
maxValue = std::max(maxVal ue, input);
minValue = std::min(minVal ue, input);
...

}

Obviously B is vastly superior to the wordy, verbose, lengthy,
overwrought, wordy, redundant, and repetitive A. <g>
Even with this rewrite, I would still prefer A. I'd prefer not to do
the same thing in the loop setup as I do each iteration of the loop
(which was my main point; poorly stated as it was :) ). If all the
real code does is find the minimum and maximum then the difference is
trivial, but if something else needs to be done with the input then it
will have to be repeated; once in the loop's setup, and once in the
body of the loop.

Mar 6 '07 #8
Clark Cox wrote:
On 2007-03-05 15:31:29 -0800, Pete Becker <pe**@versatile coding.comsaid:
>Gary Wessle wrote:
>>Hi

often I need to read numbers and only keep the highest or lowest.
so I do something like
int uLimit = 0;
int lLimit = 999999999999; //hoping the compiler will not complain

uLimit = val_read uLimit ? val_read : uLimit;
lLimit = val_read < lLimit ? val_read : lLimit;

how do I choose the original lLimit?


Use the first value of val_read for both limits.


I would tend to disagree, as that would require a special case for the
first iteration of the loop.
But there _is_ a special condition for the first value - namely that
there be one! You cannot have a well-defined max or min of zero items.
So read the first value, check that you can get that first value and
complain somehow (err message, exception, etc.) if you can't, then make
it the starting running value for both max and min, and then have a loop
for any remaining values. That is the only way that is consistent with
the concept of what a max or a min actually is. Picking some hocum
'safe' starting value, even the min or max allowable value for the data
type, will give a broken answer (an answer claiming to be correct, but
which isn't) when there are no input items.

--
Ron House ho***@usq.edu.a u
http://www.sci.usq.edu.au/staff/house
Ethics website: http://www.sci.usq.edu.au/staff/house/goodness
Mar 6 '07 #9
cl*******@gmail .com wrote:
On Mar 5, 5:33 pm, Pete Becker <p...@versatile coding.comwrote :
>Clark Cox wrote:
>>On 2007-03-05 15:31:29 -0800, Pete Becker <p...@versatile coding.comsaid:
Gary Wessle wrote:
Hi
often I need to read numbers and only keep the highest or lowest.
so I do something like
int uLimit = 0;
int lLimit = 999999999999; //hoping the compiler will not complain
uLimit = val_read uLimit ? val_read : uLimit;
lLimit = val_read < lLimit ? val_read : lLimit;
how do I choose the original lLimit?
Use the first value of val_read for both limits.
I would tend to disagree, as that would require a special case for the
first iteration of the loop.
i.e. I would prefer A to B:
Err, they do two different things. And, of course, they're written to
make A look better.
>>/*A*/
int maxValue = std::numeric_li mits<int>::min( );
int minValue = std::numeric_li mits<int>::max( );
while( ... )
{
int input = getNextValue();
maxValue = std::max(maxVal ue, input);
minValue = std::min(minVal ue, input);
...
}
/*B*/
int input = getNextValue();
int maxValue = input;
int minValue = input;
do
{
input = getNextValue();
maxValue = std::max(maxVal ue, input);
minValue = std::min(minVal ue, input);
}
while(...);
Let me present two versions that are biased the other way:

/*A*/
maxValue = std::numeric_li mits<int>::min( );
minValue = std::numeric_li mits<int>::max( );

while (...)
{
int input = getNextValue();
maxValue = std::max(maxVal ue, input);
minValue = std::min(minVal ue, input);
...

}

/*B*/
maxValue = minValue = getNextValue();

while (...)
{
int input = getNextValue();
maxValue = std::max(maxVal ue, input);
minValue = std::min(minVal ue, input);
...

}

Obviously B is vastly superior to the wordy, verbose, lengthy,
overwrought, wordy, redundant, and repetitive A. <g>

Even with this rewrite, I would still prefer A. I'd prefer not to do
the same thing in the loop setup as I do each iteration of the loop
(which was my main point; poorly stated as it was :) ). If all the
real code does is find the minimum and maximum then the difference is
trivial, but if something else needs to be done with the input then it
will have to be repeated; once in the loop's setup, and once in the
body of the loop.
But all that was asked for was to find the minimum or the maximum, so
the possibility that one approach may be better than another in some
other situation isn't particularly pertinent. I'm really not interested
in generating a tutorial on when to do what, just in suggesting possible
approaches.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Mar 6 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3156
by: Bob Murdoch | last post by:
I'm developing an intranet application using W2k server and IE5.5 - 6.x clients. I've got one particular function that calls a stored procedure to update a number of records, depending on user input. The problem we have run into is that this procedure can take a few minutes to complete in certain circumstances, and of course IIS times out. ...
10
3459
by: ThunderMusic | last post by:
Hi, How can I get if the Windows running is a server version? Is there a property somewhere that can tell me if the Windows version is a server edition (including server, advanced server, web server, etc) or just a workstation (if it's not a workstation, then it's a server). My app must verifies that for security reasons. It should work...
3
1684
by: ChadDiesel | last post by:
I'm new to Access and need some advice. I am trying to setup a database to print labels and reports for our shipments. I have set up a table with fields such as I have taken a snapshot of what I would like to do: http://www.chadworld.com/images/form.jpg All of these fields are in a table called: Ship_Info
1
2858
by: ChadDiesel | last post by:
I'm new to access and need some help. I have a form with a shipment destination at the top. I then have a subform that I enter boxes for that shipment. I have several shipments going at a time, so I can use the control at the bottom of the form to move from shipment to shipment and I can see all the boxes in each shipment. I'd like to...
1
3226
by: u473 | last post by:
I am scratching my head with the required quotes and parentheses. I started with an existing working Query with Running Total by date. Now I need to produce a running total by quarter day, so I converted my dates in serial increments of 6 hours intervals. I verified that these serial dates have the expected values. I run into repeated...
6
2930
by: KashMarsh | last post by:
Trying to show running totals on a report, except it needs to show one total amount and values being subtracted from it. For example, the report shows a Total Inventory amount (TotInvAmt). And then amounts for each month for the orders like JanAmt, FebAmt, etc. I want to show under each month column the following: (TotInvAmt)-(JanAmt)=B ...
3
1279
by: papaja | last post by:
Hello, My table has 2 columns with some numbers: col1 col2 --------------- 5 0 0 5 0 10 2 0 8 0
7
7846
by: mark.a.lemoine | last post by:
Our application currently interfaces to SQL Server and Oracle, we're implementing it into DB2 currently and I have a question to ask. First a little background - We're interfacing into DB2 v8 on zOS (I hope the terminology is right - ours is a Windows client app, and the DB2 is at a customer site and they are taking care of the server),...
11
21971
by: Gregor Kovaè | last post by:
Hi! I have an SQL script with bunch of CREATE TABLES, ALTER TABLES, ... I'd like it to run transactially. I have found that id I run a ALTER STATEMENT inside a transaction and then roll it back the changes disapear. So if this works for one statement it should also for a script, right ? I run the script with the following command: db2cmd...
8
1516
by: John | last post by:
Hi I am using the below code to run a sql on an underlying access table; insStr = "INSERT INTO ( Action, , , Request_Date ) " & _ "VALUES (""Modify Client"", 93, ""Administrator"", Now())" Dim insCmd As New OleDbCommand(insStr, dbConContacts)
0
7452
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7387
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7643
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7798
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7738
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5956
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5316
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3441
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3436
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.