package gve.calc.formula;

public class AnalysisFunctions implements Function {
	public AnalysisFunctions() {}
	
	public Part call(String funcname,Part args,Evaluator eval) {
		if (funcname.equals("abs"))
			return computeAbs(eval,args.evaluate(eval));
		return null;
	}
	
	public static Part computeAbs(Evaluator ev,Object a) {
		if (a instanceof Real) {
			return new Real(Math.abs( ((Real)a).doubleValue ));
		} else if (a instanceof Interval) {
			Interval intv = (Interval)a;
			if (intv.low >= 0) return new Interval(intv.low,intv.high,intv.openLow,intv.openHigh);
			if (intv.high >= 0) {	// low < 0 < high
				if (intv.high > -intv.low) return new Interval(0,intv.high,false,intv.openHigh);
				else return new Interval(0,-intv.low,false,intv.openLow);
			}
			// else low < high < 0
			return new Interval(-intv.high,-intv.low,intv.openHigh,intv.openLow);
		} else return null;
	}
}