What is the faster way to check whether 2 floats have the same sign?

Started by
11 comments, last by Kwizatz 17 years, 8 months ago
Hello, Anyone know what is the fastest/cool way to check whether 2 floats is the same sign? Nachi
Nachi Lau (In Christ, I never die)www.sky-dio.com/Nachi
Advertisement
check the sign bits?

I am no bit twiddler, but here is some code to play around with:

#include <iostream>#include <string.h>int main (int argc, char * const argv[]) {    float f = -118.625;    unsigned long src;        memcpy(&src, &f, sizeof(float));        int s;    s = (src & 0x80000000UL) >> 31;        std::cout << s << std::endl;        return 0;}


[Edited by - visage on August 9, 2006 5:57:17 PM]
Do you care about telling the difference between negative zero and positive zero?
.
EDIT: WAY OFF!

No clue, read visage's post.

My thoughts:

First, float variables are 32 bit, 1 bit for the sign, 8 for the exponent, and 23 for fraction. This should work, since the sign byte is the highest bit in the float variable. Reference.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
float x;
float y;
(x==0&&y==0) || (((*(unsigned*)&x)&0x80000000) ^ ((*(unsigned*)&y)&0x80000000))
actually... second thoughts :)

bool sameSign = (x*y >= 0);
I would say also, that the mult would be the easiest to read, easiest to maintain, and almost the fastest. Problem with the mult is that of course, you may lose precision....

-.00000000001 * 0.00000000001 ~ 0 = 0
-.00000000001 * -.000000000001 ~ 0 = 0

Quite possibly true on the mult...

What about:

bool sameSign = ((x>0) + (y>0) - (x<0) - (y<0) != 0);

It treats 0 as being the same sign as -1 and 1... which I suppose you'd want...
That last one has a bug if x==0 and y==0.
I would interpret both floats as unsigned ints, xor them together and test for the sign bit (this might be different on different platforms, since it depends on endianness).

It looks like this:
bool have_same_sign(float x, float y){  unsigned a=*reinterpret_cast<unsigned *>(&x);  unsigned b=*reinterpret_cast<unsigned *>(&y);  return !((a^b)&0x80000000u);}

This topic is closed to new replies.

Advertisement