473,387 Members | 1,321 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Rejecting negative number

Take a class like the following:

class Finger
{
public:

double length;
}
I'm writing reusable code and, being dictator, I dictate that "length"
cannot be negative. The following would be perfect:

unsigned double length;

But ofcourse doesn't compile.
So... I've come up with 3 potential solutions: A1 A2 B .
A) Create 2 separate member functions, GetLength and SetLength. SetLength
will reject negative numbers. When supplied with a negative number it will:
A1) Throw an exception
A2) Return a false boolean value indicating failure

B) Just document that the code will have undefined behaviour if length is
set to a negative number.

Which would you suggest? Any other suggestions?
I myself am leaning toward B.
Thanks
-JKop

Jul 22 '05 #1
16 5020

"JKop" <NU**@NULL.NULL> wrote in message
news:_u***************@news.indigo.ie...
Take a class like the following:

class Finger
{
public:

double length;
}
I'm writing reusable code and, being dictator, I dictate that "length"
cannot be negative. The following would be perfect:

unsigned double length;

But ofcourse doesn't compile.
So... I've come up with 3 potential solutions: A1 A2 B .
A) Create 2 separate member functions, GetLength and SetLength. SetLength
will reject negative numbers. When supplied with a negative number it will: A1) Throw an exception
A2) Return a false boolean value indicating failure

B) Just document that the code will have undefined behaviour if length is
set to a negative number.

Which would you suggest? Any other suggestions?
I myself am leaning toward B.


A1 without a doubt.

Also note that A1 is a subset of B, so you could document B but implement A1
if you really wanted to. When your users complain about the exception, you
just say 'I told you the behaviour was undefined'.

john
Jul 22 '05 #2
John Harrison posted:
A1 without a doubt.

Also note that A1 is a subset of B, so you could document B but
implement A1 if you really wanted to. When your users complain about
the exception, you just say 'I told you the behaviour was undefined'.

Thanks for the input.
I myself think that that would be too kind. "Undefined behaviour" means
exactly what it says on the tin. It's like saying to a child, "Yes, Go play
with the matches and the petrol - Just don't come running to _ME_ when
you've burned yourself". And ofcourse, any responsible parent would say,
"Don't play with the matches and the petrol!" and then actually physically
prevent the children from performing such actions.

But... we're all consenting adults here.
Although I still haven't made my decision yet.
All input appreciated.
Thanks
-JKop
Jul 22 '05 #3
JKop wrote:
Take a class like the following:

class Finger
{
public:

double length;
}
I'm writing reusable code and, being dictator, I dictate that "length"
cannot be negative. The following would be perfect:

unsigned double length;

But ofcourse doesn't compile.
So... I've come up with 3 potential solutions: A1 A2 B .
A) Create 2 separate member functions, GetLength and SetLength.
SetLength will reject negative numbers. When supplied with a negative
number it will: A1) Throw an exception
A2) Return a false boolean value indicating failure

B) Just document that the code will have undefined behaviour if
length is set to a negative number.

Which would you suggest? Any other suggestions?
I myself am leaning toward B.
Thanks
-JKop


Make a small property template class that allows for range validation, like
this:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

template <class T, class Modify>
class property
{
protected:
T var;
Modify mod;
public:
property<T, Modify>(const T& init, const Modify& m)
: mod(m), var(init)
{}

operator T()
{
return var;
}

property& operator =(const T& val)
{
if(!mod(val))
throw invalid_argument("bad property argument.");
var = val;
return *this;
}
};

template<class T>
struct RangeValidationPropModified
{
string msg;
T minval, maxval;
public:
RangeValidationPropModified<T>(string m, const T& minv, const T& maxv)
: msg(m),
minval(minv),
maxval(maxv)
{}

bool operator() (const T& val)
{
if(val >= minval && val <= maxval)
{
cout << msg;
return true;
}
else
return false;
}
};

int main(int argc, char* argv[])
{
property<int, RangeValidationPropModified<int> >
p(0, RangeValidationPropModified<int>("the property is being
modifed!\n", 0, 100));

cout << p << endl;
p = 5;
cout << p << endl;
p = 10;
cout << p << endl;
try
{
p = -1;
cout << p << endl;
p = 1000;
cout << p << endl;
}
catch(invalid_argument ex)
{
cout << ex.what() << endl;
}

system("pause");
return 0;
}

- Pete
Jul 22 '05 #4
Petec wrote:
JKop wrote: <snip>
operator T()
{
return var;
}
Should be:

operator T&()
{
return var;
}

To allow operators to be applied.

<snip>
Jul 22 '05 #5
*
?
"Petec" <x@x.x> wrote in message
news:CW****************@newsread2.news.pas.earthli nk.net...
Petec wrote:
JKop wrote:

<snip>

operator T()
{
return var;
}


Should be:

operator T&()
{
return var;
}

To allow operators to be applied.

<snip>

Jul 22 '05 #6
* wrote:
?
"Petec" <x@x.x> wrote in message
news:CW****************@newsread2.news.pas.earthli nk.net...
Petec wrote:
JKop wrote:

<snip>

operator T()
{
return var;
}


Should be:

operator T&()
{
return var;
}

To allow operators to be applied.

<snip>


??

Please don't top-post, BTW.

- Pete
Jul 22 '05 #7
"JKop" <NU**@NULL.NULL> wrote in message
news:_u***************@news.indigo.ie...
unsigned double length;

But ofcourse doesn't compile. A) Create 2 separate member functions, GetLength and SetLength. SetLength
will reject negative numbers. When supplied with a negative number it will: A1) Throw an exception
A2) Return a false boolean value indicating failure

B) Just document that the code will have undefined behaviour if length is
set to a negative number.

Which would you suggest? Any other suggestions?


First choice = B for performance reasons, simplicity of code. The C++
standard does it all the time (ie. if iterator points to 2 plus end then
result is undefined, if std::sort compare function does not have strict weak
ordering then undefined behavior).

Second choice = A1. As a variation, only if NDEBUG is defined then do we
throw an exception.

Third choice = A2.
Jul 22 '05 #8
"Petec" <x@x.x> wrote in message news:<CW****************@newsread2.news.pas.earthl ink.net>...
Petec wrote:
JKop wrote:

<snip>

operator T()
{
return var;
}


Should be:

operator T&()
{
return var;
}

To allow operators to be applied.


No. If the restriction is that the value should be positive,
one cannot allow T::operator*=, someonce could pass -1. The same
applies for operator+=, opertaor/=, and (for positive arguments)
to operator-=.

The correct form is operator T() const;, as it won't modify var.

Regards,
Michiel Salters
Jul 22 '05 #9
Michiel Salters wrote:
"Petec" <x@x.x> wrote in message
news:<CW****************@newsread2.news.pas.earthl ink.net>...
Petec wrote:
JKop wrote: <snip>

operator T()
{
return var;
}


Should be:

operator T&()
{
return var;
}

To allow operators to be applied.


No. If the restriction is that the value should be positive,
one cannot allow T::operator*=, someonce could pass -1. The same
applies for operator+=, opertaor/=, and (for positive arguments)
to operator-=.

The correct form is operator T() const;, as it won't modify var.


Ahh, yes, I forgot about that. I'll implement the operators on it and post
back here...

- Pete

Regards,
Michiel Salters


Jul 22 '05 #10
> > A) Create 2 separate member functions, GetLength and
SetLength. SetLength will reject negative numbers. When
supplied with a negative number it

will:

A1) Throw an exception
A2) Return a false boolean value indicating failure

B) Just document that the code will have undefined
behaviour if length is set to a negative number.

Which would you suggest? Any other suggestions?


First choice = B for performance reasons, simplicity of
code. The C++ standard does it all the time (ie. if
iterator points to 2 plus end then result is undefined,
if std::sort compare function does not have strict weak
ordering then undefined behavior).


I see your point, but it sounds a bit like premature
optimization. Not to mention that most of the projects
I have worked on don't have documentation nearly as
thorough or complete or up to date as the holy standard
library. In my experience causing a hard failure is a
better way to go (exception) -- unless profiling tells
you there is a performance problem, in which case
considering the "documented undefined behavior" route
may be justified.
Jul 22 '05 #11
JKop wrote:
Take a class like the following:

class Finger
{
public:

double length;
}
I'm writing reusable code and, being dictator, I dictate that "length"
cannot be negative. The following would be perfect:

unsigned double length;

But ofcourse doesn't compile.
So... I've come up with 3 potential solutions: A1 A2 B .
A) Create 2 separate member functions, GetLength and SetLength. SetLength
will reject negative numbers. When supplied with a negative number it will:
A1) Throw an exception
A2) Return a false boolean value indicating failure

B) Just document that the code will have undefined behaviour if length is
set to a negative number.

Which would you suggest? Any other suggestions?
I myself am leaning toward B.
Thanks
-JKop


From an alternative perspective, define length in terms of the
smallest unit, then use an unsigned long or unsigned integer.
For example, one might use millimeters for length:
class Finger
{
unsigned int length; // in millimeters.
};

To get more pedantic, one might employ a template property
for the unit of measurement.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #12
template<class Class>
Class GeneratePositive(const Class& object)
{
return ( (object > 0) ? object : -object );
}
-JKop
Jul 22 '05 #13
"JKop" <NU**@NULL.NULL> wrote in message news:wFluc.911
template<class Class>
Class GeneratePositive(const Class& object)
{
return ( (object > 0) ? object : -object );
}


This is a fine solution for a certain problem. But most times I want the
program to error out when I give in wrong input, not to correct my wrong
input. This keeps the logic in my own code clear, which helps understanding
and maintaining the code.

Though to make it general, should we replace the "0" in object>0 with
"Class()"? A generic class may not have a conversion from zero as an
integer to a Class object, namely an implicit constructor Class::Class().
Jul 22 '05 #14
In article <_u***************@news.indigo.ie>, JKop <NU**@NULL.NULL>
wrote:
Take a class like the following:

class Finger
{
public:

double length;
}
I'm writing reusable code and, being dictator, I dictate that "length"
cannot be negative. The following would be perfect:

unsigned double length;

But ofcourse doesn't compile.
So... I've come up with 3 potential solutions: A1 A2 B .
A) Create 2 separate member functions, GetLength and SetLength. SetLength
will reject negative numbers. When supplied with a negative number it will:
A1) Throw an exception
A2) Return a false boolean value indicating failure

B) Just document that the code will have undefined behaviour if length is
set to a negative number.

Which would you suggest? Any other suggestions?


As with any other interface question, what would be easer for the
client? If the client code isn't any different no matter what you
choose, then choose the one that is easiest to implement (ie 'B'.)
At the very least, make sure that the behavior is defined in the debug
version of the program...

class Finger {
double _length;
public:
double length() { return _length; }
void length( double l ) { assert( l >= 0 ); _length = l; }
};

This is what I would recomend.
Jul 22 '05 #15


Another idea:

template<class Class> class Unsigned
{
private:

Class object;
public:

operator Class(void)
{
return object;
}

Class& operator=(const Class& in_object)
{
return ( object = ( (in_object > 0) ? in_object : -in_object) ) ;
}
};
int main(void)
{
Unsigned<double> diameter;

FunctionThatTakesADouble(diameter);

return 0;
}

One problem though is in specifying arguments to the constructor, and
accessing member functions and member variables. Then I considered the
following:


template<class Class> class Unsigned : public Class
{
Class& operator=(const Class& in_object)
{
return ( object = ( (in_object > 0) ? in_object : -in_object) ) ;
}
};

But then ofcourse, C++ limits the programmer from inheriting from intrinsic
types. Also, the operator= of the base class will have to be virtual.
Any thoughts?
-JKop
Jul 22 '05 #16
JKop posted:
Also, the operator= of the base class will have to be virtual.

Wrong about that.
-JKop
Jul 22 '05 #17

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

Similar topics

13
by: Ron | last post by:
Hi all I'm deciding whether to use the PK also as an account number, invoice number, transaction number, etc that the user will see for the respective files. I understand that sometimes a...
6
by: | last post by:
i have a field that holds currency formatted numbers. how would I make it so that all values entered in this field would be negative? thanks in advance
5
by: Subrahmanyam Arya | last post by:
Hi Folks , I am trying to solve the problem of reading the numbers correctly from a serial line into an intel pentium processor machine . I am reading 1 byte and 2byte data both positive...
11
by: tlyczko | last post by:
Hello Rob B posted this wonderful code in another thread, http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/c84d8538025980dd/6ead9d5e61be85f0#6ead9d5e61be85f0 I could not...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
39
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
20
by: Casey | last post by:
Is there an easy way to use getopt and still allow negative numbers as args? I can easily write a workaround (pre-process the tail end of the arguments, stripping off any non-options including...
3
by: Peng Yu | last post by:
Hi, I don't understand why rbegin() -rend() gives a negative number. Since rbegin() + 1 gives the one before the last element, I think rbegin() - rend() should give a positive number. ...
1
by: santoshsri | last post by:
Hi All, My C# web application calls a webservice to process a report. It sends XMLs as parameter and in response gets an XML node which stores Binay datatype bin.base64. It makes an instance of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.