Sign of integer function.

J

Jason Heyes

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.
 
V

Victor Bazarov

Jason said:
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
 
P

Peter_Julian

| 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;
}
 
J

Jason Heyes

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.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Jason said:
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....".
 
P

Peter_Julian

| >
| > 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::eek:stream&
operator<<(std::eek:stream&, const SignedInteger&);
};

std::eek:stream&
operator<<(std::eek:stream& 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;
std::cout << std::endl;
}
return 0;
}

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

Victor Bazarov

Peter_Julian said:
Jason Heyes said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top