Thread: negative to posative

  1. #1
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    negative to posative

    How can i make -a into +a?

  2. #2
    lurker
    Guest
    Multiply by -1.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Or you can use the absolute value function.
    http://www.cprogramming.com/fod/abs.html
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by gamer4life687
    Or you can use the absolute value function.
    http://www.cprogramming.com/fod/abs.html
    that page is 100% wrong. there is no abs() which takes and returns an int!
    hello, internet!

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    If you want to change the sign on any variable just do:
    Code:
    a=-a;
    But if you only want to change negatives to positives, not the other way around, then use the absolute value function.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Code:
    Originally posted by Moi
    that page is 100% wrong. there is no abs() which takes and returns an int

    Its funny you say it doesnt work. I tested the exact program on that site and it works 100%!. Maybe your compiler doesnt have that.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by gamer4life687
    Its funny you say it doesnt work. I tested the exact program on that site and it works 100%!. Maybe your compiler doesnt have that. [/B]
    Right, you should mention your compiler on for example
    mathematical questions

  8. #8
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    think before you speak

    of course the program will work, ints and doubles can be implicitly converted around. that is not what i said though.
    hello, internet!

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Simply return 0 - x.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    To get the inverse of a number...

    Code:
    //To get the (-) negative equiv. of 100 (-100) just reverse the bits and add 1, 
    //same for negative to positive.
    
    int main(){
    	int i = 100;
            i = ~i + 1;
    	cout<<i<<endl;
    	return 0;
    }
    OR Multiply by -1.
    Last edited by OneStiffRod; 12-27-2002 at 08:15 PM.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  11. #11
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    ahh, i see. there is an int abs (int), but its in <stdlib.h>, not <math.h>. therefore the flaw in the tutorial program is #including the wrong header file, because without <stdlib.h> int abs (int) is not in scope, and double abs (double) is used with implicit conversions (although if <stdlib.h> had been there it would use an overloaded abs() instead). happy now?
    hello, internet!

  12. #12
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Interesting what we can come up with with only one arguement. I sure hope that KrappyKoder responds to this and gets his answer. Now that we know how to do the change plz lets not argue anymore. I get the point.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  13. #13
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    No, clearly the best solution is the following:

    Code:
    int a = -4394;
    
    for (int i = 0; i < a*-2; i++)
    a++;

  14. #14
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    meh

    The method i ended up using was from a friend, playing with unsigned and signed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unsigned negative problem :s
    By nas_am in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2009, 07:35 AM
  2. Convert bytes to int, keep the negative number
    By umm in forum C Programming
    Replies: 7
    Last Post: 03-20-2009, 04:47 PM
  3. Can we input negative numbers in the output windiw
    By hitesh1511 in forum C Programming
    Replies: 1
    Last Post: 08-22-2006, 12:07 AM
  4. Negative Numbers
    By Quantrizi in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2003, 12:48 AM
  5. how to handle integer overflow in C
    By kate1234 in forum C Programming
    Replies: 8
    Last Post: 04-23-2003, 12:20 PM