Thread: super fast question

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    27

    super fast question

    how do you eliminate the - sign of an negative integer regardless if the integer is positive or negative

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The abs function declared in <stdlib.h> or <math.h>.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Code:
    if (i < 0) {
      i = -i;
    }
    ...or even...
    i = abs(i);
    DavT
    -----------------------------------------------

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    36
    or you can use if
    Code:
    if (x < 0) 
               x = -x;
    or
    Code:
    x = (x > 0)? x : (-x);

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    you can use the fbas funciotn as well

    fabs(-8.75) -- > 8.75

    synatx : - fabs( value) --> declared in <math.h>


    s.s.harish

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    x-=(x+-1);

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Quantum1024
    x-=(x+-1);

    Well, it works perfectly for x = 1 and x = -1. Did you test it for other values of x?

    (I hope this isn't the beginning of a continuing thread that shows more and more baroque expressions that set things equal to 1. I really hate that stuff.)

    Regards,

    Dave

    "If it ain't baroque, don't fix it."
    Last edited by Dave Evans; 03-15-2005 at 10:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question again, this one should be fast!
    By Akkernight in forum C++ Programming
    Replies: 12
    Last Post: 02-23-2009, 10:29 AM
  2. fast simple question
    By Akkernight in forum C++ Programming
    Replies: 27
    Last Post: 01-26-2009, 09:29 AM
  3. With super powers would you:
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 09-17-2003, 11:27 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM