0

In Java I can distinguish between 0D and -0D:

new Double("0").equals(new Double("-0")) // false

But apparently this seems not to work anymore after GWT transpiled my Java to JavaScript. I even get false in GWT dev-mode but true in GWT super-dev-mode.

I've read that in JavaScript

0.0 === -0.0 // true

but

Object.is(0, -0) // false

How can I force GWT to use the Object.is comparision? Or is there any other solution to check if I got a negative or positive zero?

Community
  • 1
  • 1
Sebastian
  • 5,721
  • 3
  • 43
  • 69

2 Answers2

1

You could declare a new method using JSNI that uses Object.is:

public static native boolean compareDoubles(Double a, Double b) /*-{
    return Object.is(a, b);
}-*/;
Stik
  • 519
  • 4
  • 17
  • +1 This seems to work but when I use `Object.is` in a GwtTestCase I get `com.google.gwt.core.client.JavaScriptException: (null)` – Sebastian Feb 10 '17 at 12:04
  • Hmm it's possible it won't behave the same under the hosted `HtmlUnit` browser. I've certainly seen cases where behaviour is different. I've not got my environment set up to investigate at the moment, but i'll try and have a look this evening – Stik Feb 10 '17 at 12:12
0

OK, after some more research, I came up with a solution using JSNI:

native boolean isNegative(double value)
/*-{
    return 1 / value < 0;
}-*/;

As 1/-0 produces -Infinity in JavaScript, this one works.

Sebastian
  • 5,721
  • 3
  • 43
  • 69