How to invert an inputted number?

This is a discussion on How to invert an inputted number? within the C++ Programming forums, part of the General Programming Boards category; Hello, and I think this is a very basic question, but I don't know how to take the negative of ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    128

    How to invert an inputted number?

    Hello, and I think this is a very basic question, but I don't know how to take the negative of a number as inputted by the user. I am trying to make a program to do the quadratic formula, in which the user enters a number (b) and the program inverts it to get -b which is then used in the equation. Is it as simple as putting:

    Code:
    topline1 = -b + (sqrt(discriminant));
    ...or do I need to do something more complicated?

    Thanks!

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    The way you've done it is fine, but you need to be a bit careful so that the result doesn't overflow (signed overflow is undefined). This typically involves making sure that you're not negating INT_MIN or similar.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Guess My Number (Need help)
    By dhardin in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2009, 12:59 PM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 01:56 PM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 05:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21