public static NodeValue unaryPlus(NodeValue nv) { // Not quite a no-op - tests for a number NumericType opType = classifyNumeric("unaryPlus", nv) ; return nv ; }
@Override public NodeValue exec(NodeValue v) { switch (XSDFuncOp.classifyNumeric("exp10", v)) { case OP_INTEGER: int x = v.getInteger().intValue() ; if ( x >= 0 ) return NodeValue.makeInteger(BigInteger.TEN.pow(x)) ; // Anything else -> double //$FALL-THROUGH$ case OP_DECIMAL: case OP_FLOAT: case OP_DOUBLE: return NodeValue.makeDouble(Math.pow(10, v.getDouble())) ; default: throw new ARQInternalErrorException("Unrecognized numeric operation : "+ v) ; } } }
@Override public NodeValue exec(NodeValue v) { switch (XSDFuncOp.classifyNumeric("sq", v)) { case OP_INTEGER: BigInteger i = v.getInteger(); return NodeValue.makeInteger( i.pow(2) ); case OP_DECIMAL: double dec = v.getDecimal().doubleValue() ; return NodeValue.makeDecimal( Math.pow(dec, 2d)) ; case OP_FLOAT: // TODO Should squaring a float keep it a float? case OP_DOUBLE: return NodeValue.makeDouble( Math.pow(v.getDouble(), 2d) ) ; default: throw new ARQInternalErrorException("Unrecognized numeric operation : "+v) ; } }
@Override public NodeValue exec(NodeValue v) { switch (XSDFuncOp.classifyNumeric("cube", v)) { case OP_INTEGER: BigInteger i = v.getInteger(); return NodeValue.makeInteger( i.pow(3) ); case OP_DECIMAL: double dec = v.getDecimal().doubleValue() ; return NodeValue.makeDecimal( Math.pow(dec, 3d)) ; case OP_FLOAT: // TODO Should cubing a float keep it a float? case OP_DOUBLE: return NodeValue.makeDouble( Math.pow(v.getDouble(), 3d) ) ; default: throw new ARQInternalErrorException("Unrecognized numeric operation : "+v) ; } }
public static NodeValue floor(NodeValue v) { switch (classifyNumeric("floor", v)) { case OP_INTEGER : return v ; case OP_DECIMAL : BigDecimal dec = v.getDecimal().setScale(0, BigDecimal.ROUND_FLOOR) ; return NodeValue.makeDecimal(dec) ; case OP_FLOAT : return NodeValue.makeFloat((float)Math.floor(v.getFloat())) ; case OP_DOUBLE : return NodeValue.makeDouble(Math.floor(v.getDouble())) ; default : throw new ARQInternalErrorException("Unrecognized numeric operation : " + v) ; } }
public static NodeValue ceiling(NodeValue v) { switch (classifyNumeric("ceiling", v)) { case OP_INTEGER : return v ; case OP_DECIMAL : BigDecimal dec = v.getDecimal().setScale(0, BigDecimal.ROUND_CEILING) ; return NodeValue.makeDecimal(dec) ; case OP_FLOAT : return NodeValue.makeFloat((float)Math.ceil(v.getFloat())) ; case OP_DOUBLE : return NodeValue.makeDouble(Math.ceil(v.getDouble())) ; default : throw new ARQInternalErrorException("Unrecognized numeric operation : " + v) ; } }
public static NodeValue abs(NodeValue nv) { switch (classifyNumeric("abs", nv)) { case OP_INTEGER : return NodeValue.makeInteger(nv.getInteger().abs()) ; case OP_DECIMAL : return NodeValue.makeDecimal(nv.getDecimal().abs()) ; case OP_FLOAT : return NodeValue.makeFloat(Math.abs(nv.getFloat())) ; case OP_DOUBLE : return NodeValue.makeDouble(Math.abs(nv.getDouble())) ; default : throw new ARQInternalErrorException("Unrecognized numeric operation : " + nv) ; } }
public static NodeValue round(NodeValue v) { switch (classifyNumeric("round", v)) { case OP_INTEGER: return v; case OP_DECIMAL: int sgn = v.getDecimal().signum(); BigDecimal dec; if (sgn < 0) dec = v.getDecimal().setScale(0, BigDecimal.ROUND_HALF_DOWN); else dec = v.getDecimal().setScale(0, BigDecimal.ROUND_HALF_UP); return NodeValue.makeDecimal(dec); case OP_FLOAT: return NodeValue.makeFloat(Math.round(v.getFloat())); case OP_DOUBLE: return NodeValue.makeDouble(Math.round(v.getDouble())); default: throw new ARQInternalErrorException("Unrecognized numeric operation : " + v); } }
@Override public NodeValue exec(NodeValue v1, NodeValue v2) { switch (XSDFuncOp.classifyNumeric("pow", v1)) { case OP_INTEGER: BigInteger i = v1.getInteger(); return NodeValue.makeInteger( i.pow(v2.getInteger().intValue()) ); case OP_DECIMAL: double dec = v1.getDecimal().doubleValue() ; return NodeValue.makeDecimal( Math.pow(dec, v2.getDouble())) ; case OP_FLOAT: // TODO Should raising a float to a power keep it a float? case OP_DOUBLE: return NodeValue.makeDouble( Math.pow(v1.getDouble(), v2.getDouble()) ) ; default: throw new ARQInternalErrorException("Unrecognized numeric operation : "+ v1) ; } }
switch (vlSpClass) { case VSPACE_NUM: NumericType type = classifyNumeric("javaSprintf", nvValue); switch (type) { case OP_DECIMAL:
public static NodeValue unaryMinus(NodeValue nv) { switch (classifyNumeric("unaryMinus", nv)) { case OP_INTEGER : return NodeValue.makeInteger(nv.getInteger().negate()) ; case OP_DECIMAL : return NodeValue.makeDecimal(nv.getDecimal().negate()) ; case OP_FLOAT : return NodeValue.makeFloat(-nv.getFloat()) ; case OP_DOUBLE : return NodeValue.makeDouble(-nv.getDouble()) ; default : throw new ARQInternalErrorException("Unrecognized numeric operation : " + nv) ; } }
public static int compareNumeric(NodeValue nv1, NodeValue nv2) { NumericType opType = classifyNumeric("compareNumeric", nv1, nv2) ; switch (opType) { case OP_INTEGER : return calcReturn(nv1.getInteger().compareTo(nv2.getInteger())) ; case OP_DECIMAL : return calcReturn(nv1.getDecimal().compareTo(nv2.getDecimal())) ; case OP_FLOAT : return calcReturn(Float.compare(nv1.getFloat(), nv2.getFloat())) ; case OP_DOUBLE : return calcReturn(Double.compare(nv1.getDouble(), nv2.getDouble())) ; default : throw new ARQInternalErrorException("Unrecognized numeric operation : (" + nv1 + " ," + nv2 + ")") ; } }
public static NodeValue roundXpath3(NodeValue v, NodeValue precision, boolean isHalfEven) { if(!precision.isInteger()){ throw new ExprEvalTypeException("The precision for rounding should be an integer"); } int precisionInt = precision.getInteger().intValue(); String fName = isHalfEven ? "round-half-to-even" : "round"; switch (classifyNumeric(fName, v)) { case OP_INTEGER : BigDecimal decFromInt = roundDecimalValue(new BigDecimal(v.getInteger()),precisionInt,isHalfEven); return NodeValue.makeInteger(decFromInt.toBigIntegerExact()); case OP_DECIMAL : return NodeValue.makeDecimal(roundDecimalValue(v.getDecimal(),precisionInt,isHalfEven)) ; case OP_FLOAT : BigDecimal decFromFloat = roundDecimalValue(new BigDecimal(v.getFloat()),precisionInt,isHalfEven); return NodeValue.makeFloat(decFromFloat.floatValue()) ; case OP_DOUBLE : BigDecimal decFromDouble = roundDecimalValue(new BigDecimal(v.getDouble()),precisionInt,isHalfEven); return NodeValue.makeDouble(decFromDouble.doubleValue()) ; default : throw new ARQInternalErrorException("Unrecognized numeric operation : " + v) ; } }
public static NodeValue numSubtract(NodeValue nv1, NodeValue nv2) { switch (classifyNumeric("subtract", nv1, nv2)) { case OP_INTEGER : return NodeValue.makeInteger(nv1.getInteger().subtract(nv2.getInteger())) ; case OP_DECIMAL : return NodeValue.makeDecimal(nv1.getDecimal().subtract(nv2.getDecimal())) ; case OP_FLOAT : return NodeValue.makeFloat(nv1.getFloat() - nv2.getFloat()) ; case OP_DOUBLE : return NodeValue.makeDouble(nv1.getDouble() - nv2.getDouble()) ; default : throw new ARQInternalErrorException("Unrecognized numeric operation : (" + nv1 + " ," + nv2 + ")") ; } }
@Override public NodeValue exec(NodeValue v1, NodeValue v2) { switch (XSDFuncOp.classifyNumeric("pow", v1, v2)) { case OP_INTEGER: BigInteger x = v1.getInteger(); int y = v2.getInteger().intValue() ; if ( y >= 0 ) return NodeValue.makeInteger( x.pow(y)) ; // Anything else -> double //$FALL-THROUGH$ case OP_DECIMAL: case OP_FLOAT: case OP_DOUBLE: double d1 = v1.getDouble() ; double d2 = v2.getDouble() ; if ( d1 == 1 && d2 == Double.POSITIVE_INFINITY ) { if ( v1.isInteger() ) return NodeValue.nvONE ; else return NodeValue.makeDouble(1) ; } return NodeValue.makeDouble( Math.pow(v1.getDouble(), v2.getDouble()) ) ; default: throw new ARQInternalErrorException("Unrecognized numeric operation : "+ v1) ; } }
public static NodeValue numAdd(NodeValue nv1, NodeValue nv2) { switch (classifyNumeric("add", nv1, nv2)) { case OP_INTEGER : return NodeValue.makeInteger(nv1.getInteger().add(nv2.getInteger())) ; case OP_DECIMAL : return NodeValue.makeDecimal(nv1.getDecimal().add(nv2.getDecimal())) ; case OP_FLOAT : return NodeValue.makeFloat(nv1.getFloat() + nv2.getFloat()) ; case OP_DOUBLE : return NodeValue.makeDouble(nv1.getDouble() + nv2.getDouble()) ; default : throw new ARQInternalErrorException("Unrecognized numeric operation : (" + nv1 + " ," + nv2 + ")") ; } }
public static NodeValue numMultiply(NodeValue nv1, NodeValue nv2) { switch (classifyNumeric("multiply", nv1, nv2)) { case OP_INTEGER : return NodeValue.makeInteger(nv1.getInteger().multiply(nv2.getInteger())) ; case OP_DECIMAL : return NodeValue.makeDecimal(nv1.getDecimal().multiply(nv2.getDecimal())) ; case OP_FLOAT : return NodeValue.makeFloat(nv1.getFloat() * nv2.getFloat()) ; case OP_DOUBLE : return NodeValue.makeDouble(nv1.getDouble() * nv2.getDouble()) ; default : throw new ARQInternalErrorException("Unrecognized numeric operation : (" + nv1 + " ," + nv2 + ")") ; } }
: new DecimalFormat(picture.getString(), dfs) ; NumericType nt = XSDFuncOp.classifyNumeric("fn:formatNumber", nv) ; String s = null ; switch(nt) {
public static NodeValue numDivide(NodeValue nv1, NodeValue nv2) { switch (classifyNumeric("divide", nv1, nv2)) { case OP_INTEGER : { if ( nv2.getInteger().equals(BigInteger.ZERO) ) throw new ExprEvalException("Divide by zero in divide") ; // Note: result is a decimal BigDecimal d1 = new BigDecimal(nv1.getInteger()) ; BigDecimal d2 = new BigDecimal(nv2.getInteger()) ; return decimalDivide(d1, d2) ; } case OP_DECIMAL : { if ( nv2.getDecimal().compareTo(BigDecimal.ZERO) == 0 ) throw new ExprEvalException("Divide by zero in decimal divide") ; BigDecimal d1 = nv1.getDecimal() ; BigDecimal d2 = nv2.getDecimal() ; return decimalDivide(d1, d2) ; } case OP_FLOAT : // No need to check for divide by zero return NodeValue.makeFloat(nv1.getFloat() / nv2.getFloat()) ; case OP_DOUBLE : // No need to check for divide by zero return NodeValue.makeDouble(nv1.getDouble() / nv2.getDouble()) ; default : throw new ARQInternalErrorException("Unrecognized numeric operation : (" + nv1 + " ," + nv2 + ")") ; } }