4

Simple question: How do i test if a value is greater or equal than positive zero (+0)? Or how do I test if it's greater than negative zero (-0)?

val >= 0 or val >= +0 or val > -0 don't do the trick.

(I need it for handling moments.js diff() output. )

Thank you!

koubic
  • 597
  • 1
  • 11
  • 23
  • 3
    There is no positive or negative zero, there's just zero ! – adeneo Dec 28 '14 at 18:52
  • 1
    @adeneo: There is. http://en.wikipedia.org/wiki/Signed_zero – Karoly Horvath Dec 28 '14 at 18:55
  • in my console -0==+0 logs true, so it is the same in javascript – cirtrus Dec 28 '14 at 18:56
  • @KarolyHorvath - not in JS, `(+0) === (-0) = true` – adeneo Dec 28 '14 at 18:56
  • 1
    @adeneo: that *doesn't mean* there's *no* signed zero. there's: `-1/Infinity` – Karoly Horvath Dec 28 '14 at 18:59
  • @KarolyHorvath - and when using logical operators, that's relevant how exactly ? – adeneo Dec 28 '14 at 19:01
  • And BTW, I can do it easier `-1 * 0` – adeneo Dec 28 '14 at 19:01
  • just a driveby comment, but see here: http://stackoverflow.com/questions/11998340/why-is-0-0-true-in-javascript?rq=1 ; there is a special implementation in Javascript for -0/+0 forcing them to be considered equal, I believe this is as per IEEE 754 (http://en.wikipedia.org/wiki/Signed_zero#Comparisons) – Stephen Byrne Dec 28 '14 at 19:02
  • @adeneo: you are asking the wrong question again and again. it's not relevant for logical operators. but *that wasn't the question*. you made that question up yourself. the problem is with comparison. have you even tried to do what the OP asked? From the wikipedia page I linked: "often denoted by −0 (negative zero) and +0 (positive zero), regarded as *equal* by the numerical comparison operations but with *possible different behaviors in particular operations*" – Karoly Horvath Dec 28 '14 at 19:02
  • 1
    My point being, testing if something is greater than positive zero, would be exactly the same as testing if something is greater than negative zero, there is no difference, and `moment` surely doesn't differentiate between the two. – adeneo Dec 28 '14 at 19:07
  • @KarolyHorvath do you have an example of a JavaScript numeric expression which compares differently to the positive and negative zero values? – Pointy Dec 28 '14 at 19:09
  • @Pointy: MichaelLu already posted such an example. – Karoly Horvath Dec 28 '14 at 19:11
  • Of course; the bitwise representation of floating-point values in JavaScript includes two values for zero, one with the sign bit set and one without. The question however is about performing comparisons in JavaScript. – Pointy Dec 28 '14 at 19:11
  • 2
    This section from [You Don't Know JavaScript](https://github.com/getify/You-Dont-Know-JS/blob/master/types%20&%20grammar/ch2.md#zeros) may help – TehShrike Dec 28 '14 at 19:12
  • @KarolyHorvath I'm not sure he did; it's true that `1/-0` is different from `1/0`, but the question is about comparing some particular number to either (or both) `0` and `-0`. Thus `1/-0` is also not greater than `-0`, so the comparison result is the same. – Pointy Dec 28 '14 at 19:15
  • @Pointy: We already know that *that* approach fails, the OP told us. You're chasing a question which is irrelevant here. – Karoly Horvath Dec 28 '14 at 19:16
  • @Pointy If you have some value `value` and want to test whether it is greater than **or equal to** `+0`, `value >= +0` will not produce the correct result when `value` is `-0`, but `1 / value > 0` _will_ produce the correct result. – JLRishe Dec 28 '14 at 19:19
  • @KarolyHorvath - You're the one who doesn't get it. `moment.diff` gives you the difference between two dates, negative values tells you that the one date comes before the other etc. When the function returns zero, there is no difference between the dates, it doesn't matter if has a negative sign in front of it, if something doesn't work in the OP's code, this isn't the issue, and even if it is, it can be solved with just doing `Math.abs( val )` as it **doesn't matter** – adeneo Dec 28 '14 at 19:19
  • @adeneo: I just verified that `moment.diff` (with, for example, an argument of `days`) does indeed return `+0` or `-0` depending on whether one date is before or after the other. – Michael Liu Dec 28 '14 at 19:21
  • @adeneo: that was your first sane sentence, though it looks like it still wasn't correct. can I at least assume that you know accept that there's signed zero? – Karoly Horvath Dec 28 '14 at 19:25
  • Wow, what a discussion. Just a showcase that moment.diff returns +0 and -0: http://jsfiddle.net/3hb65204/4/ – koubic Dec 28 '14 at 19:28
  • @MichaelLiu - it does do that if the `diff` setting isn't specific enough to give a difference, but there still is a difference, but the zero still means there's no difference in the comparison you're doing with `diff`. In other words, it's being used wrongly, and if you just need to check if one date comes before the other, you'd do `moment(a).unix() > moment(b).unix()`, not use `diff` at all. – adeneo Dec 28 '14 at 19:28
  • @JLRishe well I guess it depends on what you mean by "correct" :) Positive and negative zero are *supposed* to compare as equal, so `-0>=0` is supposed to be `true`. – Pointy Dec 28 '14 at 19:28
  • @koubic - and it does that to show you that there is no difference in days, even if there is a difference in hours etc. Relying on the zero to tell if you the date is before or after another date is the wrong use case. – adeneo Dec 28 '14 at 19:30
  • @KarolyHorvath - and no, I don't accept that there is a signed zero, there might be a difference bitwise, but when using it logically there's just one zero. – adeneo Dec 28 '14 at 19:32
  • @adeneo: You're mental... – Karoly Horvath Dec 28 '14 at 19:33
  • 1
    @Pointy Well, yes, given the specification of its comparison operators, JavaScript considers them "equal" but it does not consider them to be "the same" (otherwise it would not make a distinction between them). So if you want to check whether a value **is** `+0` **or is greater than** `+0`, you can use `1 / value > 0`, but not `value >= +0`. Nonetheless, I agree with the sentiment that this distinction should not matter for date comparisons. Perhaps OP could tell us why it matters here? It sounds like an XY Problem. – JLRishe Dec 28 '14 at 19:34
  • @KarolyHorvath - and you're rude ! – adeneo Dec 28 '14 at 19:34
  • 2
    @JLRishe it seems to be something that Moment does, though as far as I can tell it's not documented. If you have two non-equal Moment objects, and you compare based on a granularity for which they *are* equal, you get the `+0`/`-0` behavior. I wouldn't rely on that in my own code. – Pointy Dec 28 '14 at 19:42
  • @Pointy - the right answer to this question, if the OP needs millisecond precision, would be to compare the timestamps instead. moment.js returning `-0` is just a fluke from the calculations done to timestamps, and then rounded to a given precision. – adeneo Dec 28 '14 at 19:45
  • Do you think `+0` and `-0` difference in Moment.js is a mistake and will be removed in the future? I need to know if a given string (e.g. "2014-12-28") is a future date (tomorrow and so on). I could do this to avoid +/-0: http://jsfiddle.net/3hb65204/5/. Do you think it's a better solution? Thanks. – koubic Dec 28 '14 at 20:51
  • Well, I can even do it even simpler. Never mind, problem solved and thank you everybody for enriching discussion! – koubic Dec 28 '14 at 21:36

1 Answers1

6

How do I test if a number is greater than or equal to positive zero (+0)?

Check whether its reciprocal is positive:

1 / value > 0

The reciprocal of +0 is positive Infinity. The reciprocal of -0 is negative Infinity.

UPDATE: The fact that diff can return +0 or -0 doesn't seem to be documented, so I would hesitate to rely upon this behavior. Consider using isAfter or isBefore instead:

var d = '2014-12-28';
console.log(moment(d).isAfter(moment()));          // Is the date tomorrow or later?
console.log(!moment(d).isBefore(moment(), 'day')); // Is the date today or later?
Michael Liu
  • 52,147
  • 13
  • 117
  • 150