443,705 Members | 2,017 Online Need help? Post your question and get tips & solutions from a community of 443,705 IT Pros & Developers. It's quick & easy.

# Sign of integer function.

 P: n/a 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 Replies

 P: n/a 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

 P: n/a "Jason Heyes" 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

 P: n/a > 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

 P: n/a 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

 P: n/a "Jason Heyes" 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 #include #include 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 = pos 0 vsi = neg 0 vsi = pos 10 vsi = neg 10 */ Nov 22 '05 #6

 P: n/a Peter_Julian wrote: "Jason Heyes" 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 discussion thread is closed

Replies have been disabled for this discussion. 