4

How to detect if positive or nagative changes ?

Example :

1 change to 2 = false

0 change to 1 = false

Because both are positive numbers

1 change to -1 = true

0 change to -1 = true

Because positive change to negative

-1 change to 0 = true

-1 change to 1 = true

Because negative change to positive

I do like..

var a_test,b_test;
if(a<0) {
    a_test = 'negative';
} else {
    a_test = 'positive';
}
if(b<0) {
    b_test = 'negative';
} else {
    b_test = 'positive';
}

if(a_test!==b_test) {
    alert('Yeah!');
}

For test : http://jsfiddle.net/e9QPP/

Any better coding for do something like this ?


Wiki : A negative number is a real number that is less than zero

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
l2aelba
  • 21,591
  • 22
  • 102
  • 138
  • What do you precisely *want* if one of them is 0 and not the other one ? – Denys Séguret Jan 10 '14 at 14:01
  • @dystroy Since 0 is neither positive or negative, perhaps this question would be better phrased as `Compare sign-sameness of two numbers` or `Check if one number is negative and the other number is non-negative`. – crush Jan 10 '14 at 15:04

5 Answers5

5

According to the Zen of Python,

Readability counts.

So, I present more readable and code-review passing version

if (a < 0 && b >= 0 || a >= 0 && b < 0) {
    alert("Yeah");
}
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • Yes ! , Simple but best for me :D – l2aelba Jan 10 '14 at 14:05
  • 1
    yes, considering the special case of 0, it's probably the clearest one. +1 from me – Denys Séguret Jan 10 '14 at 14:09
  • 1
    Surprisingly to me this [**beats**](http://jsperf.com/checking-for-sign-sameness) my bit shifting technique in all versions of `IE` and `Safari 5.1`. Bit shifting wins in `Chrome`, `Firefox`, and `Opera`, but this comes in second. So not only is this solution readable, it's very fast. – crush Jan 10 '14 at 14:53
  • oh fastest is @crush ! – l2aelba Jan 10 '14 at 15:00
  • 1
    Only in certain browsers. Overall, I think `thefourtheye` has the best solution. – crush Jan 10 '14 at 15:01
4

You seem to want

if (a*b<0) alert('Yeah!');

If you want to consider 0 as a positive number, you may use

if (a*b<0 || (!(a*b) && a+b<0)) alert('Yeah!');
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
3

Taking a suitable sign function:

function sign(x) {
  return typeof x === 'number' ? x ? x < 0 ? -1 : 1 : x === x ? 0 : NaN : NaN;
}

Then your problem can be expressed in clear and (hopefully) simple terms:

if (sign(a) !== sign(b)) {
  // Your code here
}
Community
  • 1
  • 1
Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
  • A link to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign would be nice. – Tibos Jan 10 '14 at 14:06
  • @Tibos, this answer does not use `Math.sign()` – epascarello Jan 10 '14 at 14:10
  • @epascarello perhaps it should. If we all come up with our own slightly different functions that do the same thing, we end up with a big interoperability mess. When there is a standard function, it should be used and a compatible alternative should be provided. – Tibos Jan 10 '14 at 14:13
1

Based on your criteria, it seems you simply want to compare the signs of the two numbers. The sign is stored in the highest bit of the number, therefore, to compare the signs, we can simply shift all the other bits off the number, and compare the signs.

Numbers in JavaScript are 64 bit (double), so we need to shift off the 63 bits preceding the sign:

if (a >>> 63 !== b >>> 63) alert('Yeah!');

Here is a jsFiddle demo

Here is a jsPerf comparison based on the 4 methods offered here.

Please note that this assumes that the numbers are 64 bit. I don't know if the spec restricts it to 64-bit, but it's plausible that there are browsers out there (or will be one day) where numbers are represented by perhaps a 128-bit number or greater.

crush
  • 16,713
  • 9
  • 59
  • 100
1

Maybe a bit late, but I had the same problem.

Assuming you now that a and b are numbers why not:

if(a < 0 ? b >=0 : b < 0){
   // your code here
}
Eddieke
  • 301
  • 3
  • 4