Using standard math functions with SPARQL¶

GraphDB supports standard math functions to be used with SPARQL.

The following query summarizes all implemented math functions:

PREFIX f: <http://www.ontotext.com/sparql/functions/>

SELECT * {

# acos
# The arccosine function. The input is in the range[-1, +1]. The output is in the range [0, pi] radians.
BIND (f:acos(0.5) AS ?acos)

# asin
# The arcsine function. The input is in the range[-1, +1]. The output is in the range [-pi/2, pi/2] radians.
BIND (f:asin(0.5) AS ?asin)

# atan
# The arctangent function. The output is in the range (-pi/2, pi/2) radians.
BIND (f:atan(1) AS ?atan)

# atan2
# The double-argument arctangent function (the angle component of the conversion from rectangular coordinates to polar coordinates), see Math.atan2().
# The output is in the range [-pi/2, pi/2] radians.
BIND (f:atan2(1, 0) AS ?atan2)

# cbrt
# The cubic root function.
BIND (f:cbrt(2) AS ?cbrt)

# copySign
# Returns the first floating-point argument with the sign of the second floating-point argument, see Math.copySign().
BIND (f:copySign(2, -7.5) AS ?copySign)

# cos
# The cosine function. The argument is in radians.
BIND (f:cos(1) AS ?cos)

# cosh
# The hyperbolic cosine function.
BIND (f:cosh(1) AS ?cosh)

# e
# The E constant, the base of the natural logarithm.
BIND (f:e() AS ?e)

# exp
# The exponent function, e^x.
BIND (f:exp(2) AS ?exp)

# expm1
# The Math.expm1() function. Returns e^x - 1.
BIND (f:expm1(3) AS ?expm1)

# floorDiv
# Returns the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.
# The arguments are implicitly cast to long.
BIND (f:floorDiv(5, 2) AS ?floorDiv)

# floorMod
# Returns the floor modulus of the int arguments. The arguments are implicitly cast to long.
BIND (f:floorMod(10, 3) AS ?floorMod)

# getExponent
# Returns the unbiased exponent used in the representation of a double.
# This means that we take N from the binary representation of X: X = 1 * 2^N + {1|0} * 2^(N-1) + ... + {1|0} * 2^0,
# i.e. the power of the highest non-zero bit of the binary form of X.
BIND (f:getExponent(10) AS ?getExponent)

# hypot
# Returns sqrt(x^2 +y^2) without intermediate overflow or underflow.
BIND (f:hypot(3, 4) AS ?hypot)

# IEEEremainder
# Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard.
BIND (f:IEEEremainder(3, 4) AS ?IEEEremainder)

# log
# The natural logarithm function.
BIND (f:log(4) AS ?log)

# log10
# The common (decimal) logarithm function.
BIND (f:log10(4) AS ?log10)

# log1p
# The Math.log1p() function.
# Returns the natural logarithm of the sum of the argument and 1.
BIND (f:log1p(4) AS ?log1p)

# max
# The greater of two numbers.
BIND (f:max(3, 5) AS ?max)

# min
# The The smaller of two numbers.
BIND (f:min(3, 5) AS ?min)

# nextAfter
# Returns the floating-point number adjacent to the first argument in the direction of the second argument.
BIND (f:nextAfter(2, -7) AS ?nextAfter)

# nextDown
# Returns the floating-point value adjacent to d in the direction of negative infinity.
BIND (f:nextDown(2) AS ?nextDown)

# nextUp
# Returns the floating-point value adjacent to d in the direction of positive infinity.
BIND (f:nextUp(2) AS ?nextUp)

# pi
# The Pi constant.
BIND (f:pi() AS ?pi)

# pow
# The power function.
BIND (f:pow(2, 3) AS ?pow)

# rint
# Returns the double value that is closest in value to the argument and is equal to a mathematical integer.
BIND (f:rint(2.51) AS ?rint)

# scalb
# Returns x × 2^scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set.
BIND (f:scalb(3, 3) AS ?scalb)

# signum
# Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.
BIND (f:signum(-5) AS ?signum)

# sin
# The sine function. The argument is in radians.
BIND (f:sin(2) AS ?sin)

# sinh
# The hyperbolic sine function.
BIND (f:sinh(2) AS ?sinh)

# sqrt
# The square root function.
BIND (f:sqrt(2) AS ?sqrt)

# tan
# The tangent function. The argument is in radians.
BIND (f:tan(1) AS ?tan)

# tanh
# The hyperbolic tangent function.
BIND (f:tanh(1) AS ?tanh)

# toDegrees
# Converts an angle measured in radians to an approximately equivalent angle measured in degrees.
BIND (f:toDegrees(1) AS ?toDegrees)

# Converts an angle measured in degrees to an approximately equivalent angle measured in radians.

# ulp
# Returns the size of an ulp of the argument.
# An ulp, unit in the last place, of a double value is the positive distance between this floating-point value and the double value next larger in magnitude. Note that for non-NaN x, ulp(-x) == ulp(x). See Math.ulp().
BIND (f:ulp(1) AS ?ulp)

}


Note

All variables in the BIND clauses should be bound.

The result of the query evaluatuion is:

acos="1.0471975511965979"^^<http://www.w3.org/2001/XMLSchema#double>
asin="0.5235987755982989"^^<http://www.w3.org/2001/XMLSchema#double>
atan="0.7853981633974483"^^<http://www.w3.org/2001/XMLSchema#double>
atan2="1.5707963267948966"^^<http://www.w3.org/2001/XMLSchema#double>
cbrt="1.2599210498948732"^^<http://www.w3.org/2001/XMLSchema#double>
copySign="-2.0"^^<http://www.w3.org/2001/XMLSchema#double>
cos="0.5403023058681398"^^<http://www.w3.org/2001/XMLSchema#double>
cosh="1.543080634815244"^^<http://www.w3.org/2001/XMLSchema#double>
e="2.718281828459045"^^<http://www.w3.org/2001/XMLSchema#double>
exp="7.38905609893065"^^<http://www.w3.org/2001/XMLSchema#double>
expm1="19.085536923187668"^^<http://www.w3.org/2001/XMLSchema#double>
floorDiv="2.0"^^<http://www.w3.org/2001/XMLSchema#double>
floorMod="1.0"^^<http://www.w3.org/2001/XMLSchema#double>
getExponent="3.0"^^<http://www.w3.org/2001/XMLSchema#double>
hypot="5.0"^^<http://www.w3.org/2001/XMLSchema#double>
IEEEremainder="-1.0"^^<http://www.w3.org/2001/XMLSchema#double>
log10="0.6020599913279624"^^<http://www.w3.org/2001/XMLSchema#double>
log="1.3862943611198906"^^<http://www.w3.org/2001/XMLSchema#double>
log1p="1.6094379124341003"^^<http://www.w3.org/2001/XMLSchema#double>
max="5.0"^^<http://www.w3.org/2001/XMLSchema#double>
min="3.0"^^<http://www.w3.org/2001/XMLSchema#double>
nextAfter="1.9999999999999998"^^<http://www.w3.org/2001/XMLSchema#double>
nextDown="1.9999999999999998"^^<http://www.w3.org/2001/XMLSchema#double>
nextUp="2.0000000000000004"^^<http://www.w3.org/2001/XMLSchema#double>
pi="3.141592653589793"^^<http://www.w3.org/2001/XMLSchema#double>
pow="8.0"^^<http://www.w3.org/2001/XMLSchema#double>
rint="3.0"^^<http://www.w3.org/2001/XMLSchema#double>
scalb="24.0"^^<http://www.w3.org/2001/XMLSchema#double>
signum="-1.0"^^<http://www.w3.org/2001/XMLSchema#double>
sin="0.9092974268256817"^^<http://www.w3.org/2001/XMLSchema#double>
sinh="3.626860407847019"^^<http://www.w3.org/2001/XMLSchema#double>
sqrt="1.4142135623730951"^^<http://www.w3.org/2001/XMLSchema#double>
tan="1.5574077246549023"^^<http://www.w3.org/2001/XMLSchema#double>
tanh="0.7615941559557649"^^<http://www.w3.org/2001/XMLSchema#double>
toDegrees="57.29577951308232"^^<http://www.w3.org/2001/XMLSchema#double>