Sign in to follow this  
neiluk

Quickest way to find change in sign

Recommended Posts

Im wanting to tell if two float values have the same sign or not. I was just wondering if there was a quick way of doing this, as at the minute im subtracting them both ways and having to use a load of if statements, seems like a lot of work for such a little thing, i figure there must be a well known optimum way to do this, Thanks

Share this post


Link to post
Share on other sites
Also:
bool SameSign(float a, float b)
{
return a * b > 0.0f;
}
With either method you can choose how zero is handled by using either > or >= for the conditionals.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Also:
bool SameSign(float a, float b)
{
return a * b > 0.0f;
}
With either method you can choose how zero is handled by using either > or >= for the conditionals.


0 is assumed to be positive.

Using a multiplication is not the best way to test the sign of two float - bakery2k1's solution is better - and can be optimized correctly by the compiler, if needed. If a and b are not stored in the fpu, this is a really fast compare; if they are on the FPU, the comparison might be slower - bit not slower than a multiplication.

Rewrite the function using the >= comparison:
namespace 
{
inline bool SameSign(float a, float b)
{
return (a >= 0.0f) == (b >= 0.0f);
}
};


Regards,

Share this post


Link to post
Share on other sites


As an aside, testing the sign of a floating point number is often a sign of far too much faith in the stability of floating point number arithmetic.

Floating point numbers should be viewed as "approximations" of the value you are computing.

But if you really must, I see no reason to assume zero is positive.


template<typename T>
signed char sgn( T t ) {
if (t<0) return -1;
if (t>0) return +1;
return 0;
}

double a, b;
if (sgn(a) == sgn(b)) {
// code
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel Deloget
0 is assumed to be positive.
I don't have a formal background in math, but I was under the impression that zero is neither positive nor negative. Is that not true?
Quote:
Using a multiplication is not the best way to test the sign of two float - bakery2k1's solution is better - and can be optimized correctly by the compiler, if needed. If a and b are not stored in the fpu, this is a really fast compare; if they are on the FPU, the comparison might be slower - bit not slower than a multiplication.
That's good to know (I didn't consider performance issues).

And of course NotAYakk is also correct that in practical situations where such a test might be required (polygon clipping, line intersection tests), a more robust approach is probably to be preferred. So all in all, it looks like my 'a*b' post was not very well considered :-) (I'll blame it on it being 5:00 in the morning after a long night of work...)

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
Original post by Emmanuel Deloget
0 is assumed to be positive.
I don't have a formal background in math, but I was under the impression that zero is neither positive nor negative. Is that not true?


Floating point numbers are not mathematical reals.

There are two float zeros: positive zero (0x00000000) and negative zero (0x80000000), due to the sign-exponent-mantissa representation. When you take the inverse, they respectively yield +infinity and -infinity. And yet, they compare equal.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
Original post by Emmanuel Deloget
0 is assumed to be positive.
I don't have a formal background in math, but I was under the impression that zero is neither positive nor negative. Is that not true?


Well actually in the equation y = |x| the slope at x=0 is undefined, so yes 0 is neither positive or negative. Correct me if i'm wrong. but i always treat it was positive, Especially when i use the sign in multiplications.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
Original post by Emmanuel Deloget
0 is assumed to be positive.
I don't have a formal background in math, but I was under the impression that zero is neither positive nor negative. Is that not true?

You are true. It is nonnegative and nonpositive. dixit Wolfram.

Apologies for the error.

Quote:

Well actually in the equation y = |x| the slope at x=0 is undefined, so yes 0 is neither positive or negative.

Easier definition: a positive number is > 0 (strictly), and a negative number is < 0. Using derivative is way too hard [smile]

Regards,

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
Original post by jyk
I don't have a formal background in math, but I was under the impression that zero is neither positive nor negative. Is that not true?
Floating point numbers are not mathematical reals.

There are two float zeros: positive zero (0x00000000) and negative zero (0x80000000), due to the sign-exponent-mantissa representation. When you take the inverse, they respectively yield +infinity and -infinity. And yet, they compare equal.
You may have misinterpreted my question. I do have at least a basic understanding of floating-point formats, and am familiar with the above material; my question was simply in reference to the mathematical definitions of positive and negative.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this