Thread: How to detect change in sign?

  1. #16
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    For floats you can reliably compare less than and greater than. Do not compare them for equality. But you should be able to test when the sign changes using these with no problem.

    Perhaps I don't understand the problem.

    bool SignChanged(float fPrevValue,float fCurValue)
      if ((fPrevValue<0.0f && fCurValue>0.0f) ||
          (fPrevValue>0.0f && fCurValue<0.0f) )
        return true;
      } else return false;
    This won't test for equality but if the result is right at 0.0f it will return false. It will only return true if the sign has changed. Note that due to the way that floats are represented in memory there will be some slight imprecision in this method.
    Last edited by VirtualAce; 02-18-2005 at 04:05 PM.

  2. #17
    Registered User
    Join Date
    Feb 2005
    Hi, Thanks for all of the replies. I have figured out a solution based upon the first reply to this thread. I have 2 seperate nested loops. I think that all of you know more about coding than I ever will so I won;t embarass myself with it, but here is the basic math I implemented. One loop is for when M>0 the other M<0. I therefore know if I am looking for a change from positive to negative or vice versa. When Y becomes greater than or less than 0 I stop the loop, round the number, and output. It isn't even a little elegant, but hey, chrome doesn't get you home.

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, 08: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, 05: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, 02:12 PM