Skip to content

Commit 58579b6

Browse files
committed
add math divide
1 parent e50a74a commit 58579b6

1 file changed

Lines changed: 40 additions & 29 deletions

File tree

MathOps/src/MathOps.java

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,90 @@
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;
16
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+
*/
213

314
public class MathOps {
415

5-
public static int add(int x, int y){
16+
public static int add(int x, int y) {
617

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
1021
return add(a, b);
1122
}
1223

13-
public static double sqrt(double x){
24+
public static double sqrt(double x) {
1425

1526
double y = x;
1627
double e = 1e-15;
1728

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;
2031
}
2132
return y;
2233
}
2334

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;
2738

2839
boolean negative = false;
29-
if(y<0){
40+
if (y < 0) {
3041
negative = true;
3142
y = -y;
3243
}
3344

34-
double r = pow(x, y/2);
35-
r = r*r;
45+
double r = pow(x, y / 2);
46+
r = r * r;
3647

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;
3950

4051
return r;
4152
}
4253

43-
public static int divide(int dividend, int divisor){
54+
public static int divide(int dividend, int divisor) {
4455

45-
if ( divisor == 0)
56+
if (divisor == 0)
4657
throw new ArithmeticException("divisor is 0");
4758

48-
boolean neg_dividend = (dividend <0);
49-
boolean neg_divisor = (divisor <0);
59+
boolean neg_dividend = (dividend < 0);
60+
boolean neg_divisor = (divisor < 0);
5061

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;
5566

5667
int result = 0;
5768

58-
while (dividend >= divisor){
69+
while (dividend >= divisor) {
5970
dividend -= divisor;
6071
result++;
6172

6273
}
6374

64-
if(neg_dividend^neg_divisor)
65-
result = (result^-1)+1;
75+
if (neg_dividend ^ neg_divisor)
76+
result = (result ^ -1) + 1;
6677

6778
return result;
6879

6980
}
7081

71-
public static void main(String[] args){
82+
public static void main(String[] args) {
7283

7384
System.out.println("add: " + add(6, -2));
7485
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));
7788

7889
}
7990
}

0 commit comments

Comments
 (0)