1 | package testcode;
|
---|
2 |
|
---|
3 | import java.math.BigDecimal;
|
---|
4 | import java.math.BigInteger;
|
---|
5 | import java.math.RoundingMode;
|
---|
6 | import java.security.SecureRandom;
|
---|
7 |
|
---|
8 | import org.eclipse.jdt.annotation.NonNull;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Tests static function calls and references
|
---|
12 | */
|
---|
13 | public class MathCode {
|
---|
14 | private final static @NonNull BigDecimal a = new BigDecimal("1.33");
|
---|
15 | private final static @NonNull BigDecimal b = new BigDecimal("1.67");
|
---|
16 | private static final int BITLENGTH = 16;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * @param args the arguments of the call.
|
---|
20 | */
|
---|
21 | static public void main(String[] args) {
|
---|
22 | // check that floats are correctly parsed - not as an int
|
---|
23 | System.out
|
---|
24 | .println(((Float) 0f).toString().equals("0.0") ? "ok0" : "ko0");
|
---|
25 |
|
---|
26 | System.out.println(-1.0e0 == -1d ? "ok1" : "ko1");
|
---|
27 | System.out.println(1f == 1d ? "ok2" : "ko2");
|
---|
28 | System.out.println(1 == +1 ? "ok3" : "ko3");
|
---|
29 |
|
---|
30 | System.out.println(Math.abs(-1) == 1 ? "ok4" : "ko4");
|
---|
31 | System.out.println(Math.abs(-1.5) == 1.5 ? "ok5" : "ko5");
|
---|
32 |
|
---|
33 | System.out.println(Math.floor(-1.5) == -2 ? "ok6" : "ko6");
|
---|
34 |
|
---|
35 | double v = Math.random();
|
---|
36 | System.out.println(v >= 0 && v <= 1 ? "ok7" : "ko7");
|
---|
37 |
|
---|
38 | // test of casst and test of 1f (should translate to either 1. or float(1)
|
---|
39 | // NOTE disable "remove unnecessary casts" fro the save actions!
|
---|
40 | System.out.println(((Object) 1f) instanceof Float ? "ok8" : "ko8");
|
---|
41 |
|
---|
42 | System.out.println(a.add(b).toString().equals("3.00") ? "ok9" : "ko9");
|
---|
43 |
|
---|
44 | System.out.println(a.divide(b, 8, RoundingMode.HALF_UP).toString()
|
---|
45 | .equals("0.79640719") ? "ok10" : "ko10");
|
---|
46 |
|
---|
47 | // smoke test, no actual check of the resulting number
|
---|
48 | final @NonNull BigInteger nr = new BigInteger(BITLENGTH,
|
---|
49 | new SecureRandom()).mod(BigInteger.valueOf(BITLENGTH));
|
---|
50 | System.out.println("ok11");
|
---|
51 |
|
---|
52 | System.out.println(
|
---|
53 | BigDecimal.ZERO.compareTo(BigDecimal.ZERO) == 0 ? "ok12"
|
---|
54 | : "ko12");
|
---|
55 | System.out.println(
|
---|
56 | BigInteger.ZERO.compareTo(BigInteger.ZERO) == 0 ? "ok13"
|
---|
57 | : "ko13");
|
---|
58 |
|
---|
59 | System.out.println(BigDecimal.TEN.divide(BigDecimal.valueOf(2)));
|
---|
60 |
|
---|
61 | System.out.println(1 / 2);
|
---|
62 | System.out.println(1d / 2);
|
---|
63 |
|
---|
64 | System.out.println(
|
---|
65 | new BigDecimal("0.123456").setScale(3, RoundingMode.HALF_UP));
|
---|
66 | System.out.println(
|
---|
67 | new BigDecimal("0.003456").setScale(3, RoundingMode.HALF_UP));
|
---|
68 | System.out.println(
|
---|
69 | new BigDecimal("0.00456").setScale(3, RoundingMode.HALF_UP));
|
---|
70 | System.out.println(Math.sqrt(9d));
|
---|
71 | System.out.println(Math.sqrt(-1d));
|
---|
72 |
|
---|
73 | // python "/" would return 0.5
|
---|
74 | System.out.println(Float.valueOf(3 / 2).toString());
|
---|
75 | // python "/" would return -0.5, // would return -1
|
---|
76 | // FIXME known issue #384
|
---|
77 | //System.out.println(Float.valueOf(3 / -2).toString());
|
---|
78 | System.out.println(Float.valueOf(3l / 2l).toString());
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 | }
|
---|