473,388 Members | 1,600 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,388 software developers and data experts.

Sign of integer function.

Does a function exist in the standard library to compute the sign of an
integer? Example:

int sign(int v)
{
return v > 0 ? 1 : (v < 0 ? -1 : 0);
}

Thanks.
Nov 22 '05 #1
6 52641
Jason Heyes wrote:
Does a function exist in the standard library to compute the sign of
an integer? Example:

int sign(int v)
{
return v > 0 ? 1 : (v < 0 ? -1 : 0);
}


No. It's so easy to write, and some folk say that 0 is non-negative
and as such should be positive (sign == 1), so since it's difficult to
agree on the functionality, why not simply leave it to the programmer?...

V
Nov 22 '05 #2

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:43***********************@news.optusnet.com.a u...
| Does a function exist in the standard library to compute the sign of
an
| integer? Example:
|
| int sign(int v)
| {
| return v > 0 ? 1 : (v < 0 ? -1 : 0);
| }
|
| Thanks.
|

The sign of an integer is part of the variable itself, there is no need
to compute anything.

bool sign(int n)
{
return n >= 0;
}
Nov 22 '05 #3
>
The sign of an integer is part of the variable itself, there is no need
to compute anything.

bool sign(int n)
{
return n >= 0;
}


How do mathematicians decide the sign of 0 if -0 = 0? Your function says -0
is positive.
Nov 22 '05 #4
Jason Heyes wrote:
How do mathematicians decide the sign of 0 if -0 = 0? Your function says
-0 is positive.


They use symbols, as always. For example, N* can represent the non-negative
integers and N+ the strictly positive. In text, the same as the lawyer
written contracts: "In this text 'positive' means....".

--
Salu2
Nov 22 '05 #5

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:43***********************@news.optusnet.com.a u...
| >
| > The sign of an integer is part of the variable itself, there is no
need
| > to compute anything.
| >
| > bool sign(int n)
| > {
| > return n >= 0;
| > }
| >
|
| How do mathematicians decide the sign of 0 if -0 = 0? Your function
says -0
| is positive.
|

You asked specifically about Integers.
The function says nothing, there is no such thing as a negative integer
with a value of 0. So forget the function. It doesn't matter whether
sign(...) returns n > 0 or n >= 0.

An integer does not support that which you are asking for. The
mathematician has the choice of using an integer or devising his/her own
numbering system.

But beware, this means he needs to realize whats involved. In other
words: whats the result of -0 + (+0) = ??? or -10 - (+10) = ??? or +0 /
(-10) = ???. Thats an implementation detail that someone has to deal
with.

In the class below, the sign is being store in a primitive boolean
member. The value is in an unsigned integer member. I've not even
defined the operators required to support such a number system (ie:
operator+, operator-, operator=, operator+=, operator-=, operator/,
operator*, etc).

// Proj_SignedInteger.cpp
#include <iostream>
#include <ostream>
#include <vector>

class SignedInteger
{
bool m_bsign;
unsigned m_u;
public:
/* ctors */
SignedInteger() : m_bsign(true), m_u(0) { }
SignedInteger(bool b, unsigned u) : m_bsign(b), m_u(u) { }
/* copy ctor */
SignedInteger(const SignedInteger& copy)
{
m_bsign = copy.m_bsign;
m_u = copy.m_u;
}
/* d~tor */
~SignedInteger() { }
/* friends */
friend std::ostream&
operator<<(std::ostream&, const SignedInteger&);
};

std::ostream&
operator<<(std::ostream& os, const SignedInteger& si)
{
os << (si.m_bsign ? "pos " : "neg ");
os << si.m_u;
return os;
}

int main()
{
SignedInteger si_pos_0; // default ctor will suffice, pos 0
SignedInteger si_neg_0(false, 0); // neg 0
SignedInteger si_pos_10(true, 10); // pos 10
SignedInteger si_neg_10(false, 10); // neg 10

std::vector< SignedInteger > vsi; // a vector container of si
vsi.push_back(si_pos_0);
vsi.push_back(si_neg_0);
vsi.push_back(si_pos_10);
vsi.push_back(si_neg_10);

for(unsigned u = 0; u < vsi.size(); ++u)
{
std::cout << "vsi[" << u << "] = " << vsi[u];
std::cout << std::endl;
}
return 0;
}

/*
vsi[0] = pos 0
vsi[1] = neg 0
vsi[2] = pos 10
vsi[3] = neg 10
*/

Nov 22 '05 #6
Peter_Julian wrote:
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:43***********************@news.optusnet.com.a u...

The sign of an integer is part of the variable itself, there is no
need to compute anything.

bool sign(int n)
{
return n >= 0;
}


How do mathematicians decide the sign of 0 if -0 = 0? Your function
says -0 is positive.


You asked specifically about Integers.
The function says nothing, there is no such thing as a negative
integer with a value of 0. [..]


As I recall, in both signed magnitude and one's complement
representations, there is. Of course, whether it compares equal
to 0 is another issue.

V
Nov 22 '05 #7

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

Similar topics

2
by: M | last post by:
Hello I am trying to build an Oracle PL/SQL Package. Some functions should only be visible from the package, while other functions need to be called from outside. I tried the following: --...
6
by: ºa¤Ö | last post by:
i need when a user is logged in, his or her account is locked and no other ppl can login by using these account.
5
by: Corky | last post by:
This works: db2 SELECT DISTINCT PROBLEM_OBJECTS.PROBLEM_ID FROM PROBLEM_OBJECTS INNER JOIN PROBLEMS ON PROBLEM_OBJECTS.PROBLEM_ID = PROBLEMS.PROBLEM_ID WHERE INTEGER(DAYS(CURRENT DATE) -...
23
by: David Frank | last post by:
How can I write a string function that encloses the input string in quotes "string" ?? below works for the "123 operation but adding " to it clobbers the "123 main() { char...
7
by: Tee | last post by:
Hi, how do we detect if a value is a positive integer ? thanks, Tee
1
by: Kalim Julia | last post by:
does Anybody know how to define a multiple set of parameter (like OleDbCommand's parameter - there are 4 sets of parameter)
7
by: kangaroo | last post by:
Hello, I have a column of type VARCHAR, storing some generally character data. A few of the values in that column are in fact numbers represented as strings (e.g., "12345"). At some moment I...
4
by: ofmars | last post by:
Hi there, In C# I'm using a DLL import attribute to call a function from this DLL. This function was written in an older version of Vb and I know the name of this function and it should...
8
by: gregory_may | last post by:
Is it possible to return "nothing" from an Integer function? This seems to give me "0" rather than "nothing". Private Function MyFunction() As Integer Return Nothing End Function
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: 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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
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...

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.