/*
 * 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.ApfloatMath;

/**
 * A számtani-mértani közép operátorok osztálya.
 * @author Esta
 */
public class ArithmeticGeometricMean 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;
            NumberType retValue = new NumberType(this.precision,false);
            retValue.setValue(ApfloatMath.agm(a.getNumericValue(),b.getNumericValue()));
            return retValue;
        } else { //mátrix >> exception
            throw new IncompatibleNumberFormatException("ERROR_AGM");
        }
    }

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

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