-2

The following macro is to determine if two numbers have the same sign, is for 2's complement number representation.

#define SAME_SIGNS( a, b )  (((long) ((unsigned long) a ^ (unsigned long) b)) >= 0 )

Can anyone suggest a javascript equivalent function?

3
  • In other words -- Could someone please translate that macro in javascript function? Commented Jun 28, 2013 at 22:28
  • Sorry, I misread your question. Commented Jun 28, 2013 at 22:40
  • literal translate: function sameSigns(a, b) { return (((a >>> 0) ^ (b >>> 0)) | 0) >= 0; } // But notice that a, b are FLOAT in NOT INTEGER, so the implementation is wrong somehow. Commented May 31, 2017 at 5:31

2 Answers 2

1
(a < 0 === b < 0)

or

(a * b > 0) // If one of a or b is 0, can't tell.

or

(a < 0 && b < 0 || a > 0 && b > 0)

true -> same, else different

Sign up to request clarification or add additional context in comments.

2 Comments

Shorter version with no risk of overflow return ((a<0) == (b<0)); (Adapted from Rik's answer)
Nailed it :). I suggest you to update your answer.
0

It won't be exactly the same, but how about something like !(a*b<0)?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.