Thread: Negative Numbers

  1. #1
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644

    Negative Numbers

    Does anyone know how to turn a value into a negative number?

    An example:
    Code:
    x = -(b/2(a))
    that would make x be the negative value of b/2a [incase you don't know, it's 1/2 of a quadratic function].

    And I know if b/2a comes out negative, the - sign makes it a positive, but I wish to know how to make it negative if it isn't already.

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Code:
    x = -(b/2)*(a);
    if(x>0)
     x=-x;
    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by Lynux-Penguin
    Code:
    x = -(b/2)*(a);
    if(x>0)
     x=-x;
    -LC
    Thank you.

    After posting this though, I realized that this would work:
    PHP Code:
    = -b/a;

    x2 x;
    x3 = -x
    Which does the same, but hey

  4. #4
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Been a while since I did quadratics, but isn't the equation
    -b
    ---
    2a?

    Which is -b/(2a) which is different to (-b/2)*a

    In the same way that
    (1/2) * 3 = 3/2
    1/(2*3) = 1/6
    are different. Just thought I'd mention it.

    And quantrizi, your way gives you a positive and negative of the answer, and you can't be sure which is in which unless you test. Lynux Penguins way was better because x will always be negative, like you wanted.

    BTW, -b/2a, is that the vertex? Just trying to remember.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try:
    x = -abs(b/2(a))

    Interesting, I hope someone can answer your question Stovellp.

  6. #6
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Yeah I'm pretty sure -b/2a is the X position of the vertex (highest or lowest pt) on the quadratic. Why do you want the negative version of it Quantrizi?

    I still prefer
    Code:
    x = -b/(2*a);
    if (x > 0)
      x = -x;

  7. #7
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    The full quadratic equation is
    Code:
    x= (-b+sqrt((b*b)-(4*a*c)))/(2a)
    and
    x= (-b-sqrt((b*b)-(4*a*c)))/(2a)
    You shouldn't need to do what you're doing... because x-y for negative y is the same as x+|y| where |n| is the absolute value of n.
    Away.

  8. #8
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Normally thats used to find the X intercepts of the quadratic. The
    Code:
    (b*b)-(4*a*c)
    Part of your equation is the discriminant, if thats negative there are no intercepts (theres no REAL square root of a negative number), if its 0 there is only one interecpt (that is, it touches the X axis), and if its greater than 0 there are two intercepts.

    You have the equation twice, one with + one with -. If the discriminant is 0 can you see there will only be one answer, +0 and -0 are the same. If the discriminant is < 0 both equations are invalid (unless we use an imaginary number system), and >0 there will be 2 different answers.

    The equation he asked for, -b/2a, is often used to calculate the vertex.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  2. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM