/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package expressiontree.operators.binary;

import expressiontree.ExpressionTreeElement;
import expressiontree.operands.NumberType;
import expressiontree.operators.BinaryOperator;
import expressiontree.operators.IncompatibleNumberFormatException;
import org.apfloat.Apfloat;
import org.apfloat.ApfloatMath;

/**
 * A maradékos osztás operátorok osztálya.
 * @author Esta
 */
public class DivisionWithReminder extends BinaryOperator {

    @Override
    public ExpressionTreeElement getValue() throws IncompatibleNumberFormatException {
        ExpressionTreeElement op1 = this.operands.get(0).getValue();
        ExpressionTreeElement op2 = this.operands.get(1).getValue();
        if (op1 instanceof NumberType && op2 instanceof NumberType) {
            //csak akkor lehet számolni, ha számot találtunk
            NumberType a = (NumberType) op1;
            NumberType b = (NumberType) op2;
            if (b.getNumericValue().compareTo(new Apfloat("0.0")) == 0) {
                throw new ArithmeticException("ERROR_DIV_ZERO");
            }
            NumberType retValue = new NumberType(this.precision, false);
            NumberType temp = new NumberType(this.precision, false);
            //kiszámoljuk, hogy mennyi a maradék, és kivonjuk az 'a' értékből
            temp.setValue(a.getNumericValue().subtract(ApfloatMath.fmod(a.getNumericValue(), b.getNumericValue())));
            //elosztjuk a temp-et b-vel.
            retValue.setValue(temp.getNumericValue().divide(b.getNumericValue()));
            return retValue;
        } else { //mátrix >> exception
            throw new IncompatibleNumberFormatException("ERROR_DIVWR");
        }
    }

    @Override
    public String toText(boolean b) {
        if (b) {
            return "() /% ()";
        } else {
            return "/% ";
        }
    }

    @Override
    public String getLocalizedName() {
        return java.util.ResourceBundle.getBundle("languages/operators").getString("DIV_WITH_REMINDER");
    }
}
