|
| 1 | +/* |
| 2 | +After test, there is bug for value -2147483648 (the minimus integer); |
| 3 | +as for integer -(-2147483648) is still -2147483648, not 2147483648. |
| 4 | +So we need a larger type which could contains value 2147483648 |
| 5 | +typedef long long_int; |
1 | 6 |
|
| 7 | +if the sizeof(long) is equal to sizeof(int) in some system, |
| 8 | +you may need to use long long instead |
| 9 | +
|
| 10 | +typedef long long long_int; |
| 11 | +
|
| 12 | + */ |
2 | 13 |
|
3 | 14 | public class MathOps { |
4 | 15 |
|
5 | | - public static int add(int x, int y){ |
| 16 | + public static int add(int x, int y) { |
6 | 17 |
|
7 | | - if(y==0) return x; |
8 | | - int a = x^y; // add |
9 | | - int b = (x&y)<<1; // carry |
| 18 | + if (y == 0) return x; |
| 19 | + int a = x ^ y; // add |
| 20 | + int b = (x & y) << 1; // carry |
10 | 21 | return add(a, b); |
11 | 22 | } |
12 | 23 |
|
13 | | - public static double sqrt(double x){ |
| 24 | + public static double sqrt(double x) { |
14 | 25 |
|
15 | 26 | double y = x; |
16 | 27 | double e = 1e-15; |
17 | 28 |
|
18 | | - while(Math.abs(x/y - y) > e*y){ |
19 | | - y = (x/y+y)/2.0; |
| 29 | + while (Math.abs(x / y - y) > e * y) { |
| 30 | + y = (x / y + y) / 2.0; |
20 | 31 | } |
21 | 32 | return y; |
22 | 33 | } |
23 | 34 |
|
24 | | - public static double pow(double x, int y){ |
25 | | - if (y==0) return 1; |
26 | | - if (y==1) return x; |
| 35 | + public static double pow(double x, int y) { |
| 36 | + if (y == 0) return 1; |
| 37 | + if (y == 1) return x; |
27 | 38 |
|
28 | 39 | boolean negative = false; |
29 | | - if(y<0){ |
| 40 | + if (y < 0) { |
30 | 41 | negative = true; |
31 | 42 | y = -y; |
32 | 43 | } |
33 | 44 |
|
34 | | - double r = pow(x, y/2); |
35 | | - r = r*r; |
| 45 | + double r = pow(x, y / 2); |
| 46 | + r = r * r; |
36 | 47 |
|
37 | | - if(y%2 != 0) r = r*x; |
38 | | - if(negative) r = 1/r; |
| 48 | + if (y % 2 != 0) r = r * x; |
| 49 | + if (negative) r = 1 / r; |
39 | 50 |
|
40 | 51 | return r; |
41 | 52 | } |
42 | 53 |
|
43 | | - public static int divide(int dividend, int divisor){ |
| 54 | + public static int divide(int dividend, int divisor) { |
44 | 55 |
|
45 | | - if ( divisor == 0) |
| 56 | + if (divisor == 0) |
46 | 57 | throw new ArithmeticException("divisor is 0"); |
47 | 58 |
|
48 | | - boolean neg_dividend = (dividend <0); |
49 | | - boolean neg_divisor = (divisor <0); |
| 59 | + boolean neg_dividend = (dividend < 0); |
| 60 | + boolean neg_divisor = (divisor < 0); |
50 | 61 |
|
51 | | - if(neg_dividend) |
52 | | - dividend = (dividend^-1)+1; |
53 | | - if(neg_divisor) |
54 | | - divisor = (divisor^-1)+1; |
| 62 | + if (neg_dividend) |
| 63 | + dividend = (dividend ^ -1) + 1; |
| 64 | + if (neg_divisor) |
| 65 | + divisor = (divisor ^ -1) + 1; |
55 | 66 |
|
56 | 67 | int result = 0; |
57 | 68 |
|
58 | | - while (dividend >= divisor){ |
| 69 | + while (dividend >= divisor) { |
59 | 70 | dividend -= divisor; |
60 | 71 | result++; |
61 | 72 |
|
62 | 73 | } |
63 | 74 |
|
64 | | - if(neg_dividend^neg_divisor) |
65 | | - result = (result^-1)+1; |
| 75 | + if (neg_dividend ^ neg_divisor) |
| 76 | + result = (result ^ -1) + 1; |
66 | 77 |
|
67 | 78 | return result; |
68 | 79 |
|
69 | 80 | } |
70 | 81 |
|
71 | | - public static void main(String[] args){ |
| 82 | + public static void main(String[] args) { |
72 | 83 |
|
73 | 84 | System.out.println("add: " + add(6, -2)); |
74 | 85 | System.out.println("sqrt: " + sqrt(120.0)); |
75 | | - System.out.println("pow: " + pow(2.0,-3)); |
76 | | - System.out.println("divide: " + divide(-21474,3)); |
| 86 | + System.out.println("pow: " + pow(2.0, -3)); |
| 87 | + System.out.println("divide: " + divide(-21474, 3)); |
77 | 88 |
|
78 | 89 | } |
79 | 90 | } |
0 commit comments