# Thread: How to detect change in sign?

1. ## 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.

2. 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.

3. Thanks, but the problem is detecting the change of the sign of Y, not cycling through the different values of X.

4. 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. 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. Change of sign

Code:
```if ( (old_y ^ new_y) < 0 ) {
// sign changed.
}```

7. 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.)

8. Yeah - my answer is pretty crummy considering the values are probably floats

9. I thought of this a few minutes after my other post.
Code:
```if ( y * old_y < 0 )
{
//y changed sign
}```

10. > 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!

11. Am I missing something? What's wrong with the obvious solution?
Code:
```if ((new_y >= 0) != (old_y >= 0))
{
// y changed sign
}```

12. >>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.

13. No - boolean operators return 0 for false, and 1 for true

14. 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
}```

>>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
}```