Skip to content

Commit

Permalink
Merge pull request apache#541 from afs/ebv-NaN
Browse files Browse the repository at this point in the history
JENA-1679: EBV values for NaN (double and float)
  • Loading branch information
afs authored Mar 13, 2019
2 parents 01eec4c + 182fd83 commit 66d14ed
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public abstract class NodeValue extends ExprNode
* string: length(string) > 0 is true
* numeric: number != Nan && number != 0 is true
* ref: http://www.w3.org/TR/xquery/#dt-ebv
*
*/

private static Logger log = LoggerFactory.getLogger(NodeValue.class) ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,14 @@ public static boolean booleanEffectiveValue(NodeValue nv) {
return !nv.getInteger().equals(BigInteger.ZERO) ;
if ( nv.isDecimal() )
return !nv.getDecimal().equals(BigDecimal.ZERO) ;
if ( nv.isDouble() )
return nv.getDouble() != 0.0 ;
if ( nv.isDouble() ) {
double v = nv.getDouble();
return v != 0.0d && ! Double.isNaN(v);
}
if ( nv.isFloat() ) {
float v = nv.getFloat();
return v != 0.0f && ! Float.isNaN(v);
}
NodeValue.raise(new ExprEvalException("Not a boolean effective value (wrong type): " + nv)) ;
// Does not return
return false ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,54 @@ public void testEBV8() {
assertTrue("Not EBV true: " + v, XSDFuncOp.booleanEffectiveValue(v));
}

static boolean ebvDouble(double d) {
return XSDFuncOp.booleanEffectiveValue(NodeValue.makeDouble(d));
}

@Test
public void testEBV9() {
assertTrue ( ebvDouble(0.01d) );
assertFalse( ebvDouble(0.0d) );
assertFalse( ebvDouble(-0.0d) );

assertFalse( ebvDouble(Double.NaN) );

assertTrue ( ebvDouble(Double.MIN_NORMAL) );
assertTrue ( ebvDouble(Double.MIN_VALUE) );
assertTrue ( ebvDouble(Double.MAX_VALUE) );

assertTrue ( ebvDouble(Double.POSITIVE_INFINITY) );
assertTrue ( ebvDouble(Double.NEGATIVE_INFINITY) );

Node x = NodeFactory.createLiteral("NaN", XSDDatatype.XSDdouble);
NodeValue v = NodeValue.makeNode(x);
assertFalse(XSDFuncOp.booleanEffectiveValue(v));
}

static boolean ebvFloat(float f) {
return XSDFuncOp.booleanEffectiveValue(NodeValue.makeFloat(f));
}

@Test
public void testEBV10() {
assertTrue ( ebvFloat(0.01f) );
assertFalse( ebvFloat(0.0f) );
assertFalse( ebvFloat(-0.0f) );

assertFalse( ebvFloat(Float.NaN) );

assertTrue ( ebvFloat(Float.MIN_NORMAL) );
assertTrue ( ebvFloat(Float.MIN_VALUE) );
assertTrue ( ebvFloat(Float.MAX_VALUE) );

assertTrue ( ebvFloat(Float.POSITIVE_INFINITY) );
assertTrue ( ebvFloat(Float.NEGATIVE_INFINITY) );

Node x = NodeFactory.createLiteral("NaN", XSDDatatype.XSDfloat);
NodeValue v = NodeValue.makeNode(x);
assertFalse(XSDFuncOp.booleanEffectiveValue(v));
}

private static boolean filterEBV(NodeValue nv) {
try {
return XSDFuncOp.booleanEffectiveValue(nv);
Expand Down

0 comments on commit 66d14ed

Please sign in to comment.