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

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

/**
 * A természetes alapú logaritmus operátorok osztálya.
 * @author Esta
 */
public class Logarithm extends UnaryOperator {

    @Override
    public ExpressionTreeElement getValue() throws IncompatibleNumberFormatException {
        ExpressionTreeElement op1 = this.operands.get(0).getValue();
        if (op1 instanceof NumberType) {
            //csak akkor lehet számolni, ha számot találtunk
            NumberType n = (NumberType) op1;
            NumberType retValue = new NumberType(this.precision, false);
            try {
                retValue.setValue(ApfloatMath.log(n.getNumericValue()));
            } catch (ArithmeticException arithmeticException) {
                throw new ArithmeticException("LOG_X_BELOW_ZERO");
            }
            return retValue;
        } else { //mátrix >> exception
            throw new IncompatibleNumberFormatException("ERROR_LOG");
        }
    }

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

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