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

This topic is 4549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, Anyone know what is the fastest/cool way to check whether 2 floats is the same sign? Nachi

##### Share on other sites
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]

##### Share on other sites
Do you care about telling the difference between negative zero and positive zero?

##### Share on other sites
EDIT: WAY OFF!

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.

##### Share on other sites
float x;
float y;
(x==0&&y==0) || (((*(unsigned*)&x)&0x80000000) ^ ((*(unsigned*)&y)&0x80000000))

##### Share on other sites
actually... second thoughts :)

bool sameSign = (x*y >= 0);

##### Share on other sites
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

##### Share on other sites
Quite possibly true on the mult...

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

##### Share on other sites
That last one has a bug if x==0 and y==0.

##### Share on other sites
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);}

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 11
• 9
• 9
• 45
• ### Forum Statistics

• Total Topics
634131
• Total Posts
3015721
×