Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions jena-arq/src/main/java/org/apache/jena/sparql/expr/NodeValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,35 @@ public abstract class NodeValue extends ExprNode
public static final NodeValue TRUE = NodeValue.makeNode("true", XSDboolean);
public static final NodeValue FALSE = NodeValue.makeNode("false", XSDboolean);

public static final NodeValue nvEmptyString = NodeValue.makeString("");

public static final NodeValue nvZERO = NodeValue.makeNode(NodeConst.nodeZero);
public static final NodeValue nvNegZERO = NodeValue.makeNode("-0.0e0", XSDdouble);
public static final NodeValue nvONE = NodeValue.makeNode(NodeConst.nodeOne);
public static final NodeValue nvTEN = NodeValue.makeNode(NodeConst.nodeTen);

public static final NodeValue nvDecimalZERO = NodeValue.makeNode("0.0", XSDdecimal);
public static final NodeValue nvDecimalONE = NodeValue.makeNode("1.0", XSDdecimal);

public static final NodeValue nvNaN = NodeValue.makeNode("NaN", XSDdouble);
public static final NodeValue nvINF = NodeValue.makeNode("INF", XSDdouble);
public static final NodeValue nvNegINF = NodeValue.makeNode("-INF",XSDdouble);
public static final NodeValue nvDoubleNegZERO = NodeValue.makeNode("-0.0e0", XSDdouble);
public static final NodeValue nvDoubleNaN = NodeValue.makeNode("NaN", XSDdouble);
public static final NodeValue nvDoubleINF = NodeValue.makeNode("INF", XSDdouble);
public static final NodeValue nvDoubleNegINF = NodeValue.makeNode("-INF",XSDdouble);

public static final NodeValue nvEmptyString = NodeValue.makeString("");
/** @deprecated Use {@link #nvDoubleNegZERO} */
@Deprecated
public static final NodeValue nvNegZERO = nvDoubleNegZERO;

/** @deprecated Use {@link #nvDoubleNaN} */
@Deprecated
public static final NodeValue nvNaN = nvDoubleNaN;

/** @deprecated Use {@link #nvDoubleINF} */
@Deprecated
public static final NodeValue nvINF = nvDoubleINF;

/** @deprecated Use {@link #nvDoubleNegINF} */
@Deprecated
public static final NodeValue nvNegINF = nvDoubleNegINF;

public static final String xsdNamespace = XSD+"#";

Expand Down Expand Up @@ -605,7 +621,7 @@ public boolean equals(Expr other, boolean bySyntax) {
// Java equals, not "same value" or "same term"
if ( other == null ) return false;
if ( this == other ) return true;
// This is the equality condition Jena uses - lang tags are different by case.
// This is the equality condition Jena uses
if ( ! ( other instanceof NodeValue nv) )
return false;
return asNode().equals(nv.asNode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

public class NodeValueDouble extends NodeValue {

double value = Double.NaN;
private final double value;

public NodeValueDouble(double d) { super(); value = d; }
public NodeValueDouble(double d, Node n) { super(n); value = d; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public class NodeValueFloat extends NodeValue
{
float value = Float.NaN;
private final float value;

public NodeValueFloat(float f) { super(); value = f; }
public NodeValueFloat(float f, Node n) { super(n); value = f; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* Operations relating to {@link NodeValue NodeValues}.
* <ul>
* <li>The code parts of arithmetic operations on {@link NodeValue}s.
* <li>Library code such as Argument testing
* <li>Library code such as expression string argument testing
* </ul>
* <p>
* This class is not considered to be part of the ARQ API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,50 @@ public static NodeValue round(NodeValue v) {
dec = v.getDecimal().setScale(0, RoundingMode.HALF_UP);
return NodeValue.makeDecimal(dec);
case OP_FLOAT:
return NodeValue.makeFloat(Math.round(v.getFloat()));
return NodeValue.makeFloat(roundFloat(v.getFloat()));
case OP_DOUBLE:
return NodeValue.makeDouble(Math.round(v.getDouble()));
return NodeValue.makeDouble(roundDouble(v.getDouble()));
default:
throw new ARQInternalErrorException("Unrecognized numeric operation : " + v);
}
}

private static double roundDouble(double d) {
if ( Double.isNaN(d) )
return Double.NaN;
if ( d == Double.POSITIVE_INFINITY )
return d;
if ( d == Double.NEGATIVE_INFINITY )
return d;
if ( d == -0.0e0 )
return d;
// Math.round returns a java long
long resultLong = Math.round(d);
if ( resultLong == 0 && d < 0 )
// Return -0 for round negative to 0.
return -0.0e0d;
// Cast to double by the return.
return resultLong;
}

private static float roundFloat(float f) {
if ( Float.isNaN(f) )
return Float.NaN;
if ( f == Float.POSITIVE_INFINITY )
return f;
if ( f == Float.NEGATIVE_INFINITY )
return f;
if ( f == -0.0e0f )
return f;
// Math.round returns a java long
long resultLong = Math.round(f);
if ( resultLong == 0 && f < 0 )
// Return -0 for round negative to 0.
return -0.0e0f;
// Math.round returns a java long, which is cast to float by the return.
return resultLong;
}

// The following function 'roundXpath3' implements the definition for "fn:round" in F&O v3.
// This is different to the "fn:round" in F&O v2.
// SPARQL 1.1 references F&O v2.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

package org.apache.jena.sparql.function;

import static org.apache.jena.sparql.expr.NodeValue.nvNaN;
import static org.apache.jena.sparql.expr.NodeValue.nvNegZERO;
import static org.apache.jena.sparql.expr.NodeValue.nvDoubleNaN;
import static org.apache.jena.sparql.expr.NodeValue.nvDoubleNegZERO;
import static org.apache.jena.sparql.expr.NodeValue.nvZERO;
import static org.apache.jena.sparql.expr.nodevalue.XSDFuncOp.*;
import java.math.BigDecimal;
Expand Down Expand Up @@ -273,7 +273,10 @@ private static NodeValue castToBoolean(NodeValue nv, XSDDatatype castType) {
if ( nv.isBoolean() )
return nv;
if ( nv.isNumber() ) {
if ( NodeValue.sameValueAs(nv, nvZERO) || NodeValue.sameValueAs(nv, nvNaN) || NodeValue.sameValueAs(nv, nvNegZERO) )
if ( NodeValue.sameValueAs(nv, nvZERO) )
return NodeValue.FALSE;
// sameValueAs Covers xsd:float
if ( NodeValue.sameValueAs(nv, nvDoubleNaN) || NodeValue.sameValueAs(nv, nvDoubleNegZERO) )
return NodeValue.FALSE;
return NodeValue.TRUE;
}
Expand Down
Loading
Loading