Skip to content

Commit 31932f7

Browse files
Fixed Double not working e.x. 3.0 was not treated as 3
1 parent 4424386 commit 31932f7

File tree

1 file changed

+53
-37
lines changed

1 file changed

+53
-37
lines changed

src/javacalculus/evaluator/CalcEXPAND.java

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import javacalculus.core.CALC;
66
import javacalculus.evaluator.extend.CalcFunctionEvaluator;
77
import javacalculus.exception.CalcWrongParametersException;
8+
import javacalculus.struct.CalcDouble;
89
import javacalculus.struct.CalcFunction;
910
import javacalculus.struct.CalcInteger;
1011
import javacalculus.struct.CalcObject;
@@ -49,43 +50,58 @@ public CalcObject expand(CalcObject object) {
4950
if (obj.getHeader().equals(CALC.POWER)) {
5051
CalcFunction function = (CalcFunction) obj;
5152
CalcObject firstObj = CALC.SYM_EVAL(function.get(0));
52-
CalcObject secondObj = CALC.SYM_EVAL(function.get(1));
53-
//System.out.println("This is a function in the power branch: " + function);
54-
if (firstObj instanceof CalcFunction && secondObj.isNumber() && secondObj instanceof CalcInteger) {//f(x)^k
55-
int pow = ((CalcInteger) secondObj).intValue();
56-
boolean isPowNegative = pow < 0;
57-
//System.out.println("WE ARE IN THE f(x)^k branch");
58-
if (isPowNegative) {
59-
//System.out.println("OH SNAP, this is the bottom part of a fraction!");
60-
pow = Math.abs(pow);
61-
}
62-
if (pow == 1) {
63-
return obj;
53+
CalcObject secondObjTemp = CALC.SYM_EVAL(function.get(1));
54+
CalcInteger secondObj = null;
55+
if (secondObjTemp.isNumber()) {
56+
if (secondObjTemp instanceof CalcInteger) {
57+
secondObj = (CalcInteger) secondObjTemp;
58+
} else {
59+
CalcDouble temp = (CalcDouble) secondObjTemp;
60+
if (temp.isInteger()) {
61+
//CalcDouble
62+
secondObj = new CalcInteger(((CalcDouble) secondObjTemp).bigDecimalValue().toBigInteger());
63+
}
6464
}
65-
ArrayList<CalcObject> resultFunc = new ArrayList<>();
66-
//System.out.println("This is the first part of the function " + firstObj);
67-
//System.out.println("This is the second part of the function " + secondObj);
68-
if (firstObj.getHeader().equals(CALC.ADD)) {
69-
Iterator iter = ((CalcFunction) firstObj).iterator();
70-
while (iter.hasNext()) {
71-
resultFunc.add(CALC.SYM_EVAL(CALC.EXPAND.createFunction(CALC.MULTIPLY.createFunction((CalcObject) iter.next(), firstObj))));
65+
//System.out.println("This is a function in the power branch: " + function);
66+
if (secondObj != null && firstObj instanceof CalcFunction) {//f(x)^k
67+
int pow = ((CalcInteger) secondObj).intValue();
68+
boolean isPowNegative = pow < 0;
69+
//System.out.println("WE ARE IN THE f(x)^k branch");
70+
if (isPowNegative) {
71+
//System.out.println("OH SNAP, this is the bottom part of a fraction!");
72+
pow = Math.abs(pow);
73+
}
74+
if (pow == 1) {
75+
return obj;
76+
}
77+
ArrayList<CalcObject> resultFunc = new ArrayList<>();
78+
//System.out.println("This is the first part of the function " + firstObj);
79+
//System.out.println("This is the second part of the function " + secondObj);
80+
if (firstObj.getHeader().equals(CALC.ADD)) {
81+
Iterator iter = ((CalcFunction) firstObj).iterator();
82+
while (iter.hasNext()) {
83+
resultFunc.add(CALC.SYM_EVAL(CALC.EXPAND.createFunction(CALC.MULTIPLY.createFunction((CalcObject) iter.next(), firstObj))));
84+
}
85+
} else {
86+
//System.out.println("not adding: " + firstObj);
87+
return obj;
88+
}
89+
////System.err.println(resultFunc);
90+
for (CalcObject temp : resultFunc) {
91+
factored = CALC.SYM_EVAL(CALC.ADD.createFunction(factored, temp));
7292
}
93+
for (int i = 0; i < pow - 2; i++) {
94+
factored = CALC.SYM_EVAL(CALC.EXPAND.createFunction(CALC.MULTIPLY.createFunction(firstObj, factored)));
95+
}
96+
if (isPowNegative) {
97+
factored = CALC.POWER.createFunction(factored, CALC.NEG_ONE);
98+
}
99+
//System.out.println("RESULT of f(x)^k: " + factored);
100+
return factored;
73101
} else {
74-
//System.out.println("not adding: " + firstObj);
75-
return obj;
102+
//System.out.println("SECOND NUM" + secondObj.getHeader());
103+
//System.out.println((firstObj instanceof CalcFunction) + " && " + (secondObj.isNumber()) + " && " + (secondObj instanceof CalcInteger));
76104
}
77-
////System.err.println(resultFunc);
78-
for (CalcObject temp : resultFunc) {
79-
factored = CALC.SYM_EVAL(CALC.ADD.createFunction(factored, temp));
80-
}
81-
for (int i = 0; i < pow - 2; i++) {
82-
factored = CALC.SYM_EVAL(CALC.EXPAND.createFunction(CALC.MULTIPLY.createFunction(firstObj, factored)));
83-
}
84-
if (isPowNegative) {
85-
factored = CALC.POWER.createFunction(factored, CALC.NEG_ONE);
86-
}
87-
//System.out.println("RESULT of f(x)^k: " + factored);
88-
return factored;
89105
}
90106
} else if (obj.getHeader().equals(CALC.MULTIPLY)) {
91107
ArrayList<CalcObject> allParts = giveList(CALC.MULTIPLY, obj);
@@ -144,6 +160,8 @@ public CalcObject expand(CalcObject object) {
144160
//System.out.println("RESULT of g(x)*f(x): " + factored + "\n" + CALC.SYM_EVAL(factored));
145161
return factored;
146162
}
163+
} else {
164+
//System.out.println("NOPE");
147165
}
148166
return obj;
149167
}
@@ -153,17 +171,15 @@ private ArrayList<CalcObject> giveList(CalcSymbol operator, CalcObject func) {
153171
////System.out.println(func);
154172
if (func instanceof CalcFunction && func.getHeader().equals(operator)) {
155173
ArrayList<CalcObject> funcParts = ((CalcFunction) func).getAll();
156-
for (int i = 0; i < funcParts.size(); i++) {
157-
CalcObject firstObj = funcParts.get(i);
174+
for (CalcObject firstObj : funcParts) {
158175
//if (firstObj instanceof CalcFunction && ((CalcFunction) firstObj).getHeader().equals(operator)) {
159176
list.addAll(giveList(operator, firstObj));
160177
//}
161178
}
162-
////System.out.println("LIST in loop" + list);
163179
} else {
164180
list.add(func);
165181
////System.out.println("LIST" + list);
166182
}
167183
return list;
168184
}
169-
}
185+
}

0 commit comments

Comments
 (0)