/// <summary> /// Gets the numeric value of the function in the given Evaluation Context for the given Binding ID /// </summary> /// <param name="context">Evaluation Context</param> /// <param name="bindingID">Binding ID</param> /// <returns></returns> public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID) { IValuedNode a = _leftExpr.Evaluate(context, bindingID); IValuedNode b = _rightExpr.Evaluate(context, bindingID); SparqlNumericType type = (SparqlNumericType)Math.Max((int)a.NumericType, (int)b.NumericType); switch (type) { case SparqlNumericType.Integer: return(new LongNode(null, Math.Max(a.AsInteger(), b.AsInteger()))); case SparqlNumericType.Decimal: return(new DecimalNode(null, Math.Max(a.AsDecimal(), b.AsDecimal()))); case SparqlNumericType.Float: return(new FloatNode(null, Math.Max(a.AsFloat(), b.AsFloat()))); case SparqlNumericType.Double: return(new DoubleNode(null, Math.Max(a.AsDouble(), b.AsDouble()))); default: throw new RdfQueryException("Cannot evalute an Arithmetic Expression when the Numeric Type of the expression cannot be determined"); } }
/// <summary> /// Evaluates the expression /// </summary> /// <param name="context">Evaluation Context</param> /// <param name="bindingID">Binding ID</param> /// <returns></returns> public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID) { IValuedNode temp = _expr.Evaluate(context, bindingID); if (temp == null) { throw new RdfQueryException("Cannot square a null"); } switch (temp.NumericType) { case SparqlNumericType.Integer: long l = temp.AsInteger(); return(new LongNode(null, l * l * l)); case SparqlNumericType.Decimal: decimal d = temp.AsDecimal(); return(new DecimalNode(null, d * d * d)); case SparqlNumericType.Float: float f = temp.AsFloat(); return(new FloatNode(null, f * f * f)); case SparqlNumericType.Double: double dbl = temp.AsDouble(); return(new DoubleNode(null, Math.Pow(dbl, 3))); case SparqlNumericType.NaN: default: throw new RdfQueryException("Cannot square a non-numeric argument"); } }
/// <summary> /// Compares two Nodes for Numeric Ordering /// </summary> /// <param name="x">Node</param> /// <param name="y">Node</param> /// <param name="type">Numeric Type</param> /// <returns></returns> protected virtual int NumericCompare(IValuedNode x, IValuedNode y, SparqlNumericType type) { if (x == null || y == null) { throw new RdfQueryException("Cannot evaluate numeric ordering when one or both arguments are Null"); } if (type == SparqlNumericType.NaN) { throw new RdfQueryException("Cannot evaluate numeric ordering when the Numeric Type is NaN"); } switch (type) { case SparqlNumericType.Decimal: return(x.AsDecimal().CompareTo(y.AsDecimal())); case SparqlNumericType.Double: return(x.AsDouble().CompareTo(y.AsDouble())); case SparqlNumericType.Float: return(x.AsFloat().CompareTo(y.AsFloat())); case SparqlNumericType.Integer: return(x.AsInteger().CompareTo(y.AsInteger())); default: throw new RdfQueryException("Cannot evaluate numeric equality since of the arguments is not numeric"); } }
public void ShouldSuccesfullyEvaluateFloatCastRegardlessOfCulture() { foreach (var ci in TestedCultureInfos) { TestTools.ExecuteWithChangedCulture(ci, () => { // given var cast = new FloatCast(new ConstantTerm(3.4f.ToLiteral(_graph))); // when IValuedNode valuedNode = cast.Evaluate(new SparqlEvaluationContext(new SparqlQuery(), new InMemoryDataset()), 0); // then Assert.AreEqual(3.4f, valuedNode.AsFloat()); }); } }
/// <summary> /// Calculates the Numeric Value of this Expression as evaluated for a given Binding /// </summary> /// <param name="context">Evaluation Context</param> /// <param name="bindingID">Binding ID</param> /// <returns></returns> public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID) { IValuedNode a = this._leftExpr.Evaluate(context, bindingID); IValuedNode b = this._rightExpr.Evaluate(context, bindingID); if (a == null || b == null) { throw new RdfQueryException("Cannot apply division when one/both arguments are null"); } SparqlNumericType type = (SparqlNumericType)Math.Max((int)a.NumericType, (int)b.NumericType); try { switch (type) { case SparqlNumericType.Integer: case SparqlNumericType.Decimal: //For Division Integers are treated as decimals decimal d = a.AsDecimal() / b.AsDecimal(); if (Decimal.Floor(d).Equals(d) && d >= Int64.MinValue && d <= Int64.MaxValue) { return(new LongNode(null, Convert.ToInt64(d))); } return(new DecimalNode(null, d)); case SparqlNumericType.Float: return(new FloatNode(null, a.AsFloat() / b.AsFloat())); case SparqlNumericType.Double: return(new DoubleNode(null, a.AsDouble() / b.AsDouble())); default: throw new RdfQueryException("Cannot evalute an Arithmetic Expression when the Numeric Type of the expression cannot be determined"); } } catch (DivideByZeroException) { throw new RdfQueryException("Cannot evaluate a Division Expression where the divisor is Zero"); } }
/// <summary> /// Calculates the Numeric Value of this Expression as evaluated for a given Binding /// </summary> /// <param name="context">Evaluation Context</param> /// <param name="bindingID">Binding ID</param> /// <returns></returns> public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID) { IValuedNode a = this._expr.Evaluate(context, bindingID); if (a == null) { throw new RdfQueryException("Cannot apply unary minus to a null"); } switch (a.NumericType) { case SparqlNumericType.Integer: return(new LongNode(null, -1 * a.AsInteger())); case SparqlNumericType.Decimal: decimal decvalue = a.AsDecimal(); if (decvalue == Decimal.Zero) { return(new DecimalNode(null, Decimal.Zero)); } else { return(new DecimalNode(null, -1 * decvalue)); } case SparqlNumericType.Float: float fltvalue = a.AsFloat(); if (Single.IsNaN(fltvalue)) { return(new FloatNode(null, Single.NaN)); } else if (Single.IsPositiveInfinity(fltvalue)) { return(new FloatNode(null, Single.NegativeInfinity)); } else if (Single.IsNegativeInfinity(fltvalue)) { return(new FloatNode(null, Single.PositiveInfinity)); } else { return(new FloatNode(null, -1.0f * fltvalue)); } case SparqlNumericType.Double: double dblvalue = a.AsDouble(); if (Double.IsNaN(dblvalue)) { return(new DoubleNode(null, Double.NaN)); } else if (Double.IsPositiveInfinity(dblvalue)) { return(new DoubleNode(null, Double.NegativeInfinity)); } else if (Double.IsNegativeInfinity(dblvalue)) { return(new DoubleNode(null, Double.PositiveInfinity)); } else { return(new DoubleNode(null, -1.0 * dblvalue)); } default: throw new RdfQueryException("Cannot evalute an Arithmetic Expression when the Numeric Type of the expression cannot be determined"); } }