How to detect change in sign?

This is a discussion on How to detect change in sign? within the C++ Programming forums, part of the General Programming Boards category; Hi, I am having a problem detecting the change in sign. I need to determine the root of the equation ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    4

    How to detect change in sign?

    Hi,

    I am having a problem detecting the change in sign. I need to determine the root of the equation y=mx+b.

    The key to making this work is to detect the change in the sign value of y as x is increased over a given interval. I can cycle through the values of X ok, but the trick is detecting the sign change.

    I also do not know if I am approaching the root from the positive side or the negative side.

    Does anyone know a nifty bit of code to help me do this?

    Thanks.
    Last edited by bugsmashers; 02-17-2005 at 09:10 PM.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Cycle through the x-values in a while loop with the conditional "y<0". I'm sorry if I've given too much away and taken the thinking out of it, but I couldn't think of a better way to put that.
    Last edited by sean; 02-17-2005 at 09:14 PM.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    Thanks, but the problem is detecting the change of the sign of Y, not cycling through the different values of X.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    How else do you propose to increase x over a given interval?

    edit: I fixed a mistake in my suggested conditional, if that's what was causing the confusion

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Assuming abs(y) is the absolute value of y:
    Code:
    if ( abs(y) + abs(old_y) > abs( y + old_y) )
    {
       //y changed sign
    }
    Another idea would be to check the sign bit, assuming y is an integer.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    Change of sign

    Code:
    if ( (old_y ^ new_y) < 0 ) {
        // sign changed.
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Salem's solution only works reliably with integers, though. Keep that in mind, especially as such code as yours probably uses floats.

    (If I'm not mistaken, the code works for floats until you get weird numbers whose combination yields +INF, -INF or NaN.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    Yeah - my answer is pretty crummy considering the values are probably floats
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I thought of this a few minutes after my other post.
    Code:
    if ( y * old_y < 0 )
    {
       //y changed sign
    }

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    > I thought of this a few minutes after my other post.
    Yep, that works as well - so long as you don't run into the infinities

    Actually, that probably doesn't matter since infinities are comparable to 0 - it's the NAN's and IND's you have to watch out for!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Am I missing something? What's wrong with the obvious solution?
    Code:
    if ((new_y >= 0) != (old_y >= 0))
    {
       // y changed sign
    }

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>Am I missing something? What's wrong with the obvious solution?


    As I understand it, there is a potential problem with the following:
    Code:
    if ((new_y >= 0) != (old_y >= 0))
    {
       //y changed sign
    }
    The >= operator returns a boolean value. The boolean value is gauranteed to equate to 0 if false and non-zero if not false (that is, true). However, the non-zero value need not be the same value. For example, a return value of 1 and -1 could both be used as values of not-false/true. Therefore, even if both sides of the != operator equated to true, the values may not be the same, leading to a potential problem. How much of a problem this is in reality is unclear. One would hope that within a given compiler, the return value for boolean not false/true would be the same, but I don't know whether it is gauranteed or not. If it isn't, this could be a nasty bug to find. Therefore, I try to avoid equating boolean return values. Just my two cents worth.
    You're only born perfect.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    No - boolean operators return 0 for false, and 1 for true
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by anonytmouse
    Am I missing something? What's wrong with the obvious solution?
    Code:
    if ((new_y >= 0) != (old_y >= 0))
    {
       // y changed sign
    }
    I don't quite compute at your level yet Anonytmouse, so my obvious solution was:
    Code:
    if ( (y > 0 && old_y < 0) || (y < 0 && old_y > 0) )
    {
       // y changed sign
    }

  15. #15
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by elad
    >>Am I missing something? What's wrong with the obvious solution?

    As I understand it, there is a potential problem with the following:
    Code:
    if ((new_y >= 0) != (old_y >= 0))
    {
       //y changed sign
    }
    Well, if you're paranoid and want to avoid this, you could try:
    Code:
    if ((new_y >= 0) ^ (old_y >= 0))
    {
       //y changed sign
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++builder6 change form names problem
    By Leite33 in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2008, 09:20 AM
  2. how to change static char* and not lose mem ?
    By jabka in forum C Programming
    Replies: 15
    Last Post: 09-07-2007, 06:33 PM
  3. Change Value in an array
    By beginner999 in forum C Programming
    Replies: 3
    Last Post: 01-18-2003, 07:16 AM
  4. Replies: 2
    Last Post: 11-08-2002, 03:22 AM
  5. Replies: 2
    Last Post: 09-04-2001, 03:12 PM

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