Sign of integer function.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason Heyes

    #1

    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.


  • Victor Bazarov

    #2
    Re: Sign of integer function.

    Jason Heyes wrote:[color=blue]
    > 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);
    > }[/color]

    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


    Comment

    • Peter_Julian

      #3
      Re: Sign of integer function.


      "Jason Heyes" <jasonheyes@opt usnet.com.au> wrote in message
      news:437eb338$0 $25852$afc38c87 @news.optusnet. com.au...
      | 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;
      }


      Comment

      • Jason Heyes

        #4
        Re: Sign of integer function.

        >[color=blue]
        > 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;
        > }
        >[/color]

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


        Comment

        • Julián Albo

          #5
          Re: Sign of integer function.

          Jason Heyes wrote:
          [color=blue]
          > How do mathematicians decide the sign of 0 if -0 = 0? Your function says
          > -0 is positive.[/color]

          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

          Comment

          • Peter_Julian

            #6
            Re: Sign of integer function.


            "Jason Heyes" <jasonheyes@opt usnet.com.au> wrote in message
            news:437f1481$0 $25858$afc38c87 @news.optusnet. com.au...
            | >
            | > 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_SignedInte ger.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(b ool b, unsigned u) : m_bsign(b), m_u(u) { }
            /* copy ctor */
            SignedInteger(c onst 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(s i_pos_0);
            vsi.push_back(s i_neg_0);
            vsi.push_back(s i_pos_10);
            vsi.push_back(s i_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
            */





            Comment

            • Victor Bazarov

              #7
              Re: Sign of integer function.

              Peter_Julian wrote:[color=blue]
              > "Jason Heyes" <jasonheyes@opt usnet.com.au> wrote in message
              > news:437f1481$0 $25858$afc38c87 @news.optusnet. com.au...[color=green][color=darkred]
              >>>
              >>> 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;
              >>> }
              >>>[/color]
              >>
              >> How do mathematicians decide the sign of 0 if -0 = 0? Your function
              >> says -0 is positive.
              >>[/color]
              >
              > You asked specifically about Integers.
              > The function says nothing, there is no such thing as a negative
              > integer with a value of 0. [..][/color]

              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


              Comment

              Working...