Skip to content

Commit b00a70e

Browse files
committed
// BH 2021.12.19 adds Double -0; fixes println(Double)
1 parent b889d5a commit b00a70e

File tree

6 files changed

+49
-6
lines changed

6 files changed

+49
-6
lines changed
133 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20211218193023
1+
20211219114745
133 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20211218193023
1+
20211219114745

sources/net.sf.j2s.java.core/src/test/Test_Double.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@
55
class Test_Double extends Test_{
66

77
public static void main(String[] args) {
8+
9+
10+
Double d = new Double("-0");
11+
Double d1 = Double.valueOf(-0);
12+
System.out.println(d);
13+
System.out.println(d1);
14+
System.out.println("" + d);
15+
System.out.println("" + d1);
16+
System.out.println(Double.valueOf(0) == Double.valueOf(-0));
17+
System.out.println(Double.valueOf(0).equals(Double.valueOf(-0)));
18+
System.out.println(d == Double.valueOf(-0));
19+
System.out.println(d == Double.valueOf(0));
20+
System.out.println(d1 == Double.valueOf(-0));
21+
System.out.println(d1 == Double.valueOf(-0));
22+
23+
24+
25+
assert(("" + Double.valueOf(-0)).equals("0.0"));
26+
assert(d != Double.valueOf(0));
27+
assert(d != Double.valueOf(-0));
28+
assert(("" + d).equals("-0.0"));
29+
assert(d.doubleValue() == 0);
30+
assert(("" + d.doubleValue()).equals("-0.0"));
31+
assert(1/d.doubleValue() == Double.NEGATIVE_INFINITY);
32+
33+
// noting here that in Java and JavaScript, Double.valueOf(-0) is 0, not -0
34+
// Java
35+
// and in JavaScript but not Java, "" + (-0) is "0" not "-0" -- this appears to be a mistake in JavaScript.
36+
37+
838
assert(DF.formatDecimal(-0.999999f, -4).equals("-1.000E+0"));
939
assert(DF.formatDecimal(9.999999f, -4).equals("1.000E+1"));
1040
assert(DF.formatDecimal(9.999999f, 4).equals("10.0000"));

sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
// Google closure compiler cannot handle Clazz.new or Clazz.super
99

10+
// BH 2021.12.19 adds Double -0; fixes println(Double)
1011
// BH 2021.12.15 default encoding for String.getBytes() should be utf-8.
1112
// BH 2021.08.16 fix for Interface initializing its subclass with static initialization
1213
// BH 2021.07.28 String.instantialize upgraded to use TextDecoder() if possible (not in MSIE)
@@ -3263,13 +3264,20 @@ ps.printf = ps.printf$S$OA = ps.format = ps.format$S$OA = function (f, args) {
32633264

32643265
ps.flush$ = function() {}
32653266

3266-
ps.println = ps.println$ = ps.println$O = ps.println$Z = ps.println$I = ps.println$S = ps.println$C = function(s) {
3267+
ps.println = ps.println$ = ps.println$Z = ps.println$I = ps.println$S = ps.println$C = function(s) {
32673268
s = (typeof s == "undefined" ? "" : "" + s);
32683269
checkTrace(s);
32693270
s = (typeof s == "undefined" ? "\r\n" : s == null ? s = "null\r\n" : s + "\r\n");
32703271
f(s);
32713272
};
32723273

3274+
ps.println$O = function(s) {
3275+
s = (typeof s == "undefined" ? "" : s.toString());
3276+
checkTrace(s);
3277+
s = (typeof s == "undefined" ? "\r\n" : s == null ? s = "null\r\n" : s + "\r\n");
3278+
f(s);
3279+
};
3280+
32733281
ps.println$J = function(l) {ps.println(Long.$s(l))}
32743282
ps.println$F = ps.println$D = function(f) {var s = "" + f; ps.println(s.indexOf(".") < 0 && s.indexOf("Inf") < 0 ? s + ".0" : s)};
32753283

@@ -5202,6 +5210,9 @@ function(n){
52025210
}, 1);
52035211

52045212
Clazz._floatToString = function(f) {
5213+
if (f === 0) {
5214+
return (1/f == -Infinity ? "-0.0" : "0.0");
5215+
}
52055216
var check57 = (Math.abs(f) >= 1e-6 && Math.abs(f) < 1e-3);
52065217
if (check57)
52075218
f/=1e7;
@@ -5229,7 +5240,7 @@ var maxFloat = 3.4028235E38;
52295240
var minFloat = -3.4028235E38;
52305241

52315242
m$(Float,"c$", function(v){
5232-
v || v == null || v != v || (v = 0);
5243+
v || v == null || v != v || (v == 0) || (v = 0);
52335244
if (typeof v != "number")
52345245
v = Float.parseFloat$S(v);
52355246
this.valueOf=function(){return v;}
@@ -5245,6 +5256,7 @@ m$(Float, "c$$S", function(v){
52455256
}, 1);
52465257

52475258
m$(Float, "c$$D", function(v){
5259+
v || (v = 0);
52485260
v = (v < minFloat ? -Infinity : v > maxFloat ? Infinity : v);
52495261
this.valueOf=function(){return v;}
52505262
}, 1);
@@ -5375,18 +5387,19 @@ return Clazz._floatToString(this.valueOf());
53755387
};
53765388

53775389
m$(Double, "c$$D", function(v){
5390+
v || (v = 0);
53785391
this.valueOf=function(){return v;};
53795392
}, 1);
53805393

53815394
m$(Double,"c$", function(v){
5382-
v || v == null || v != v || (v = 0);
5395+
v || v == null || v != v || (v == 0) || (v = 0);
53835396
if (typeof v != "number")
53845397
v = Double.parseDouble$S(v);
53855398
this.valueOf=function(){return v;}
53865399
}, 1);
53875400

53885401
m$(Double, ["c$$S"], function(v){
5389-
v || v == null || (v = 0);
5402+
v || v == null || (v == 0) || (v = 0);
53905403
if (typeof v != "number")
53915404
v = Double.parseDouble$S(v);
53925405
this.valueOf=function(){return v;};

0 commit comments

Comments
 (0)