Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ccf7265

Browse files
committed
fix for issue #270 -- Long.signum, Long.compareUnsigned fail
1 parent 489e12f commit ccf7265

File tree

3 files changed

+122
-6
lines changed

3 files changed

+122
-6
lines changed

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package test;
22

3-
import test.math.BigDecimal;
3+
import java.math.BigDecimal;
44

5-
import test.math.MathContext;
5+
import java.math.MathContext;
66
//import test.math.BigInteger;
77
//import test.math.MutableBigInteger;
88
//import java.util.GregorianCalendar;
@@ -12,14 +12,51 @@ public class Test_BigDec extends Test_ {
1212

1313

1414
public static void main(String[] args) {
15+
16+
int precision = 1000;
17+
18+
19+
BigDecimal factor1 = new BigDecimal("1");
20+
factor1 = factor1.setScale(precision);
21+
BigDecimal five = new BigDecimal("5");
22+
five = five.setScale(precision);
23+
24+
factor1 = factor1.divide(five, BigDecimal.ROUND_FLOOR);
25+
System.out.println(chopText(factor1.toString()));
26+
assert(factor1.toString().equals("0.2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
1527

28+
29+
30+
factor1 = new BigDecimal("1");
31+
BigDecimal di = factor1.divideToIntegralValue(five);
32+
System.out.println(di);
33+
assert(di.toString().equals("0E+1000"));
34+
35+
BigDecimal[] dr = factor1.divideAndRemainder(five);
36+
System.out.println(dr[0]);
37+
assert(dr[0].toString().equals("0E+1000"));
38+
System.out.println(dr[1]);
39+
assert(dr[1].toString().equals("1"));
40+
41+
1642
testBDDiv();
1743
testBDMul();
1844

1945
System.out.println("testBD OK");
2046

2147
}
2248

49+
private static String chopText(String s) {
50+
int nd = 50;
51+
String c = "";
52+
int n = s.length() / nd;
53+
int p = 0;
54+
for (int i = 0; i < n; i++)
55+
c += s.substring(i*nd, p = (i+1)*nd) + "\n";
56+
s = c + s.substring(p);
57+
return s;
58+
}
59+
2360
// failing testBDDiv();
2461

2562

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,82 @@ public class Test_Long extends Test_ {
3030

3131
public static void main(String[] args) {
3232

33+
long m1 = -1L;
34+
long m2 = -2L;
35+
long p2 = 2L;
36+
assert(Long.compare(m1, m2) == 1);
37+
assert(Long.compare(m2, m1) == -1);
38+
assert(Long.compareUnsigned(m1, m2) == 1);
39+
assert(Long.compareUnsigned(m2, m1) == -1);
40+
assert(Long.compareUnsigned(m1, p2) == 1);
41+
assert(Long.compareUnsigned(p2, m1) == -1);
42+
System.out.println("Long.compareUnsigned OK");
43+
44+
long a = 0x11111111111001L;
45+
long b = 0x11111111100000L;
46+
47+
long c = -(int) (a-b) + a - b;
48+
System.out.println(c);
49+
assert(c == 0);
50+
long e = a - b;
51+
System.out.println("a-b=" + (a-b));
52+
assert(e == 69633);
53+
54+
55+
// next two fail to wrap second operator
56+
57+
a = 10;
58+
b = 12;
59+
60+
c = a - b - (int) (a-b);
61+
// var c=Long.$sub(a,b - Long.$ival((Long.$sub(a,b))) );
62+
System.out.println(c);
63+
assert(c == 0);
64+
65+
e = a - b - 1;
66+
System.out.println("10-12-1=" + e );
67+
assert(e == -3);
68+
// var e=Long.$sub(a,b-1);
69+
// gives -4 ??
70+
71+
// something wrong here
72+
a = 10;
73+
b = 3;
74+
c = 12;
75+
76+
a = 0x11111111111001L;
77+
b = 0x11111111100000L;
78+
79+
c = -(int) (a-b) + a - b;
80+
81+
long d = a + b - (int) (a-b) - c; // 13 - 7 - 12 == -6
82+
// var d=Long.$sub(
83+
// Long.$sub(
84+
// Long.$add(a,b),
85+
// Long.$ival((Long.$sub(a,b)))
86+
// ),
87+
// c
88+
// );
89+
System.out.println(d);
90+
// gives 9607679204777982
91+
assert(d == 9607679204917248L);
92+
93+
// something wrong here
94+
a = 10;
95+
b = 3;
96+
c = 12;
97+
98+
d = a + b - (int) (a-b) - c; // 13 - 7 - 12 == -6
99+
// var d=Long.$sub(
100+
// Long.$sub(
101+
// Long.$add(a,b),
102+
// Long.$ival((Long.$sub(a,b)))
103+
// ),
104+
// c
105+
// );
106+
System.out.println(d);
107+
// gives 9607679204777982
108+
assert(d == -6);
33109

34110
long j = Long.parseLong("2"), k = 10, l = 1, m = 12;
35111
Long J = Long.valueOf(j);
@@ -390,6 +466,10 @@ private static void testExtended(long j, long k, long l, long m) {
390466
m = 12;
391467
j = k + l + l;
392468
assert(j == 12);
469+
470+
j = k - l - m;
471+
assert(j == -3);
472+
393473
j = k | l | 0xFL;
394474
assert(j == 15);
395475
m = 2;

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

Lines changed: 3 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 2024.02.23 fixes missing Long.signum
1011
// BH 2023.04.30 fixes issues when Info.console == window.console
1112
// BH 2023.03.01 upgrade for Java11 String, including String.isBlank() and CharSequence.lines(String) (in Java11 this is StringRoman1.lines(byte[])
1213
// BH 2023.02.12 upgrade for (asynchronous?) packaging
@@ -5084,7 +5085,8 @@ return i;
50845085
}, 1);
50855086

50865087
m$(C$, 'signum$J', function (i) {
5087-
return Long.$ival((Long.$or((Long.$sr(i,63)),(Long.$usr((Long.$neg(i)),63)))));
5088+
return Long.$sign(i);
5089+
//Long.$ival((Long.$or((Long.$sr(i,63)),(Long.$usr((Long.$neg(i)),63)))));
50885090
}, 1);
50895091

50905092
m$(C$, 'reverseBytes$J', function (i) {
@@ -5192,10 +5194,7 @@ Long.toUnsignedBigInteger$J = function(i) {
51925194
bi || (bi=(Clazz.load("java.math.BigInteger"), Clazz.new_(java.math.BigInteger.c$$S,["18446744073709551616"])));
51935195
return (i >= 0 ? bi.valueOf$J(i) : bi.valueOf$J(i).add$java_math_BigInteger(bi));
51945196
}
5195-
51965197

5197-
m$(Long,"signum$J", function(i){ return i < 0 ? -1 : i > 0 ? 1 : 0; }, 1);
5198-
51995198
Clazz._setDeclared("java.lang.Short", java.lang.Short = Short = function(){
52005199
if (arguments[0] === null || typeof arguments[0] != "object")this.c$(arguments[0]);
52015200
});

0 commit comments

Comments
 (0)