Quickest way to find change in sign
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
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.
Quote:Original post by jyk
Also:With either method you can choose how zero is handled by using either > or >= for the conditionals.bool SameSign(float a, float b){ return a * b > 0.0f;}
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,
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}
Quote:Original post by Emmanuel DelogetI 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?
0 is assumed to be positive.
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...)
Quote:Original post by jykQuote:Original post by Emmanuel DelogetI 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?
0 is assumed to be positive.
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.
Quote:Original post by jykQuote:Original post by Emmanuel DelogetI 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?
0 is assumed to be positive.
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.
Quote:Original post by jykQuote:Original post by Emmanuel DelogetI 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?
0 is assumed to be positive.
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,
Quote:Original post by FrunyYou 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.Quote:Original post by jykFloating point numbers are not mathematical reals.
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?
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement