Negate an integer

Discussion in 'C++' started by wij@seed.net.tw, May 6, 2006.

  1. Guest

    Hi:

    What is the protable way to negate an arithmetic integer?

    template<typename T>
    T negate(T t) {
    return -t; // <-- problem
    }

    On my Intel machines, taking negation like the above is not
    right if t is std::numeric_limits<T>::min()

    So the question may be reduced to: how to detect to throw an error.

    Thank you.
    IJ. Wang
     
    , May 6, 2006
    #1
    1. Advertising

  2. osmium Guest

    <> wrote:

    > What is the protable way to negate an arithmetic integer?


    I don't think a bulletproof way is possible. 0 is taken as a positive
    number. Since there are an even number of numbers that can be represented
    in binary, this is a problem.

    > template<typename T>
    > T negate(T t) {
    > return -t; // <-- problem
    > }
    >
    > On my Intel machines, taking negation like the above is not
    > right if t is std::numeric_limits<T>::min()
    >
    > So the question may be reduced to: how to detect to throw an error.


    Why not do the obvious? Perhaps I don't understand what your problem is.
     
    osmium, May 6, 2006
    #2
    1. Advertising

  3. Rolf Magnus Guest

    wrote:

    > Hi:
    >
    > What is the protable way to negate an arithmetic integer?
    >
    > template<typename T>
    > T negate(T t) {
    > return -t; // <-- problem
    > }
    >
    > On my Intel machines, taking negation like the above is not
    > right if t is std::numeric_limits<T>::min()
    >
    > So the question may be reduced to: how to detect to throw an error.


    Hmm, you mean how to detect that t is equal to
    std::numeric_limits<T>::min()? Or did I miss anything?
     
    Rolf Magnus, May 6, 2006
    #3
  4. Guest

    osmium wrote:
    >...
    >Why not do the obvious? Perhaps I don't understand what your problem is.


    template<typename T>
    T negate(T t) {
    if(t==std::numeric_limits<T>::min()) {
    throw error;
    }
    return -t;
    }

    1. I find no document clearly states when negate a value t yields
    valid result or not.
    2. The above method won't work for some machine not Intel CPU.

    IJ. Wang
     
    , May 7, 2006
    #4
  5. osmium Guest

    <> wrote:

    > osmium wrote:
    >>...
    >>Why not do the obvious? Perhaps I don't understand what your problem is.

    >
    > template<typename T>
    > T negate(T t) {
    > if(t==std::numeric_limits<T>::min()) {
    > throw error;
    > }
    > return -t;
    > }
    >
    > 1. I find no document clearly states when negate a value t yields
    > valid result or not.
    > 2. The above method won't work for some machine not Intel CPU.


    Are you implying that there is a compiler that lies about the value of
    INT_MAX and/or INT_MIN in <climits>? I based what I said on the assumption
    that they told the truth. I didn't look at your code; examining the
    variables such as those two are what I had in mind. If you want an
    alternative, you might be able to use one of the abs() functions somehow. I
    have nothing else to offer.
     
    osmium, May 7, 2006
    #5
  6. * Rolf Magnus:
    > wrote:
    >>
    >> What is the protable way to negate an arithmetic integer?
    >>
    >> template<typename T>
    >> T negate(T t) {
    >> return -t; // <-- problem
    >> }
    >>
    >> On my Intel machines, taking negation like the above is not
    >> right if t is std::numeric_limits<T>::min()
    >>
    >> So the question may be reduced to: how to detect to throw an error.

    >
    > Hmm, you mean how to detect that t is equal to
    > std::numeric_limits<T>::min()? Or did I miss anything?


    He means, how to detect whether -std::numeric_limits<T>::min() exists.

    Probably the most protable way to do that is to check whether
    -(std::numeric_limits<T>::min()+1) == std::numeric_limits<T>::max().

    Cheers.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, May 7, 2006
    #6
  7. Guest

    Alf P. Steinbach wrote:
    >..
    >He means, how to detect whether -std::numeric_limits<T>::min() exists.
    >..


    Yes, that is what I meant. Thank you. (good rephrase)

    IJ. Wang
     
    , May 7, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Mitchell

    [Q] fast way to negate?

    Rob Mitchell, Dec 13, 2005, in forum: Java
    Replies:
    0
    Views:
    378
    Rob Mitchell
    Dec 13, 2005
  2. Replies:
    3
    Views:
    606
    Paul McGuire
    Jan 9, 2005
  3. negate INT_MIN

    , Feb 10, 2007, in forum: C Programming
    Replies:
    2
    Views:
    430
    ┬Ča\\/b
    Feb 10, 2007
  4. dave_if
    Replies:
    5
    Views:
    451
    dave_if
    Jul 27, 2005
  5. Replies:
    1
    Views:
    401
    Marco Costa
    Aug 2, 2006
Loading...

Share This Page