I am trying to write a SPARQL query where I want to filter on the square of something, but I am simply unable to figure out how to square a number (x2) (except by multiplying it with itself, of course). I guessed a square root function called math:sqrt()
which works, yet nothing like math:pow
seems to exist.
How do I get the square of something in SPARQL and, more importantly, where can I read about it and other math functions such as math:sqrt
in SPARQL?
Note: This is related to my previous question: Reverse wikipedia geotagging lookup .
It's now a few years later and the SPARQL 1.1 Query Language has been published. It includes many more built-in functions than the original SPARQL query language. In addition to lots of functions on strings, RDF terms, &c., there are a number of numeric functions (the section numbers indicate the section in the linked standard):
- 17.4.4 Functions on Numerics
- 17.4.4.1 abs
- 17.4.4.2 round
- 17.4.4.3 ceil
- 17.4.4.4 floor
- 17.4.4.5 RAND
There's still no square
function here, and the quickest way to implement that will be using *
, so laalto's answer still stands. The operators that SPARQL supports are described in 17.3 Operator Mapping, and for XPath arithmetic is still just the set of +
, -
, *
, and /
.
SPARQL supports some XPath arithmetic functions such as + - * /
. They are described in the SPARQL spec.
The power function is not supported by the spec. And in any case x*x
is more efficient way to compute squares than pow(x,2)
would be.