# How to detect change in sign?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-17-2005
bugsmashers
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.
• 02-17-2005
sean
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.
• 02-17-2005
bugsmashers
Thanks, but the problem is detecting the change of the sign of Y, not cycling through the different values of X.
• 02-17-2005
sean
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
• 02-17-2005
swoopy
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.
• 02-18-2005
Salem
Change of sign

Code:

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

• 02-18-2005
CornedBee
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.)
• 02-18-2005
Salem
Yeah - my answer is pretty crummy considering the values are probably floats
• 02-18-2005
swoopy
I thought of this a few minutes after my other post.
Code:

if ( y * old_y < 0 )
{
//y changed sign
}

• 02-18-2005
Salem
> 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!
• 02-18-2005
anonytmouse
Am I missing something? What's wrong with the obvious solution?
Code:

if ((new_y >= 0) != (old_y >= 0))
{
// y changed sign
}

• 02-18-2005
>>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.
• 02-18-2005
Salem
No - boolean operators return 0 for false, and 1 for true
• 02-18-2005
swoopy
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
}

• 02-18-2005
pianorain
Quote:

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

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last